包装组件未渲染

huangapple go评论69阅读模式
英文:

Wrapped component not rendering

问题

我正在尝试根据条件来渲染一个组件。我正在使用包装函数来实现这个目标。然而,当条件满足时,组件没有渲染。

App.js
import { BrowserRouter, Routes, Route } from "react-router-dom";
import Component from "./Component";
import Wrapper from "./Wrapper";
export default function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/component" element={<Component />} />
        <Route path="/wcomponent" element={Wrapper(Component)} />
      </Routes>
    </BrowserRouter>
  );
}
Wrapper.js
const { useState } = require("react");

const Wrapper = (WrappedComp) => {
  const AuthComponent = (props) => {
    const [value, setValue] = useState(true);
    if (value) return <WrappedComp {...props} />;
    else return <div>另一个组件</div>;
  };

  return AuthComponent;
};
export default Wrapper;
Component.js
const Component = () => {
  return <div>第一个组件</div>;
};

export default Component;

包装组件未渲染

英文:

I am trying to render a component based on a condition. I am using wrapper function to do so. However the component is not rendering when condition is met.

App.js
import { BrowserRouter, Routes, Route } from &quot;react-router-dom&quot;;
import Component from &quot;./Component&quot;;
import Wrapper from &quot;./Wrapper&quot;;
export default function App() {
  return (
    &lt;BrowserRouter&gt;
      &lt;Routes&gt;
        &lt;Route path=&quot;/component&quot; element={&lt;Component /&gt;} /&gt;
        &lt;Route path=&quot;/wcomponent&quot; element={Wrapper(Component)} /&gt;
      &lt;/Routes&gt;
    &lt;/BrowserRouter&gt;
  );
}
Wrapper.js
const { useState } = require(&quot;react&quot;);

const Wrapper = (WrappedComp) =&gt; {
  const AuthComponent = (props) =&gt; {
    const [value, setValue] = useState(true);
    if (value) return &lt;WrappedComp {...props} /&gt;;
    else return &lt;div&gt; The other component &lt;/div&gt;;
  };

  return AuthComponent;
};
export default Wrapper;
Component.js
const Component = () =&gt; {
  return &lt;div&gt; The first Component &lt;/div&gt;;
};

export default Component;

包装组件未渲染

答案1

得分: 1

<Variable />

在这种情况下,你的包装器是一个HOC(高阶组件)。

基本上,你可以将其看作是一个带有参数的函数(Wrapper(Component))。你需要做的是定义一个变量并使用这个包装器变量作为组件(组件应以大写形式定义):

const WrapperComp = Wrapper(Component);

<BrowserRouter>
<Routes>
<Route path="/component" element={<Component />} />
<Route path="/wcomponent" element={<WrapperComp />} />
</Routes>
</BrowserRouter>

英文:

If you want to use an Variable as a component wrap it with arrows like

&lt;Variable /&gt;

In this case, your Wrapper is a HOC(Higher order component).

Basically, you can see it(Wrapper(Component)) is a function with param. All the things you need is define a variable with this Wrapper. and use this variable like a component (component should define in Capitalize form)

const WrapperComp = Wrapper(Component);

&lt;BrowserRouter&gt;
  &lt;Routes&gt;
    &lt;Route path=&quot;/component&quot; element={&lt;Component /&gt;} /&gt;
    &lt;Route path=&quot;/wcomponent&quot; element={&lt;WrapperComp /&gt;} /&gt;
  &lt;/Routes&gt;
&lt;/BrowserRouter&gt;

huangapple
  • 本文由 发表于 2023年6月13日 15:38:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462655.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定