将一些路由封装到React Router DOM v6的其他组件中。

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

encapsulating some routes to other component in react-router-dom v6

问题

Here is the translated code:

我正在使用React我有一个像这样的app.js文件

function App() {
  const [data, setData] = useState([]);
  useEffect(() => { ... setData([1, 2, 3]); }, []);

  return (
    <Router>
      <Routes>
        <Route element={<SomeElement />} />
        <Route index exact path="/path1" element={<SomeComponent data={data} />} />
        <Route path="/path2" element={<AnotherComponent data={data} />} />
      </Routes>
    </Router>
  );
}

我只想将使用数据的路由移到单独的组件中,因为我只想在需要时加载数据。

我想要看到类似于这样的代码:

function SeparateRoutes() {
  const [data, setData] = useState([]);
  useEffect(() => { ... setData([1, 2, 3]); }, []);

  return (
    <Fragment>
      <Route index exact path="/" element={<SomeComponent data={data} />} />
      <Route path="/path" element={<AnotherComponent data={data} />} />
    </Fragment>
  );
}

function App() {
  return (
    <Router>
      <Routes>
        <SeparateRoutes />
        <Route index exact path="/" element={<SomeComponent data={data} />} />
        <Route path="/path" element={<AnotherComponent data={data} />} />
      </Routes>
    </Router>
  );
}

我尝试了这个变体,但出现了错误:组件的子元素必须是Route或React.Fragment。


请注意,我已经将HTML实体转换成了正常的HTML标记。如果需要进一步的帮助,请告诉我。

<details>
<summary>英文:</summary>

I m using react and i have app.js like this:

function App() {
const [data, setData] = useState([]);
useEffect(() => { ... setData([1,2,3]);}, []);

return (
<Router>
<Routes>
<Route element={<SomeElement/>}>
<Route index exact path="/path1" element={<SomeComponent data={data}/>}/>
<Route path="/path2" element={<AnotherComponent data={data}/>}/>
</Routes>
<Router/>);
}

I just want to move routes, which are using data to separate component, since i want to load data only if i need.



I would like to see something like this: 

function SeparateRoutes(){
const [data, setData] = useState([]);
useEffect(() => { ... setData([1,2,3]);}, []);

return <Fragment>
<Route index exact path="/" element={<SomeComponent data={data}/>}/>
<Route path="/path" element={<AnotherComponent data={data}/>}/> </Fragment>;
}

function App() {

return (
<Router>
<Routes>
<SeparateRoutes/>
<Route index exact path="/" element={<SomeComponent data={data}/>}/>
<Route path="/path" element={<AnotherComponent data={data}/>}/>
</Routes>
<Router/>);
}

I`ve tried this variant but there is an error: component children must be a Route or React.Fragment

</details>


# 答案1
**得分**: 1

```jsx
如果你只是尝试将代码分割并在组件中分别呈现路由,那么每组 `Route` 组件都需要在一个 `Routes` 组件中渲染。

示例:

```jsx
import { Routes, Route } from 'react-router-dom';

function SeparateRoutes() {
  const [data, setData] = useState([]);

  useEffect(() => {
    ...
    setData([1, 2, 3]);
  }, []);

  return (
    <Routes>
      <Route path="/path1" element={<SomeComponent data={data} />} />
      <Route path="/path2" element={<AnotherComponent data={data} />} /> 
    </Routes>
  );
}

确保你没有意外地渲染了一个自闭合的 Router 组件标签,它应该包裹所有需要访问它提供的路由上下文的路由和 UI 内容。

import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';

function App() {
  return (
    <Router>
      <SeparateRoutes />
      <Routes>
        <Route path="/" element={<SomeComponent />} />
        <Route path="/path" element={<AnotherComponent />} />
      </Routes>
    </Router>
  );
}

如果你只想为特定的路由获取数据,你也可以尝试创建一个布局路由并呈现嵌套路由,而不是完全分开的具有后代路由的组件。

示例:

import { Outlet } from 'react-router-dom';

function DataLayout() {
  const [data, setData] = useState([]);

  useEffect(() => {
    ...
    setData([1, 2, 3]);
  }, []);

  // 在 Outlet 上下文中传递 data 状态
  return <Outlet context={data} />;
}
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';

function App() {
  return (
    <Router>
      <Routes>
        <Route path="/" element={<SomeComponent />} />
        <Route path="/path" element={<AnotherComponent />} />
        <Route element={<DataLayout />}>
          <Route path="/path1" element={<SomeComponent />} />
          <Route path="/path2" element={<AnotherComponent />} /> 
        </Route>
      </Routes>
    </Router>
  );
}
import { useOutletContext } from 'react-router-dom';

export const AnotherComponent = () => {
  const data = useOutletContext();

  ...
};
英文:

If you are just trying to split the code up a bit and render routes separately in components then each set of Route components need to be rendered in a Routes component.

Example:

import { Routes, Route } from &#39;react-router-dom&#39;;

function SeparateRoutes() {
  const [data, setData] = useState([]);

  useEffect(() =&gt; {
    ...
    setData([1, 2, 3]);
  }, []);

  return (
    &lt;Routes&gt;
      &lt;Route path=&quot;/path1&quot; element={&lt;SomeComponent data={data} /&gt;} /&gt;
      &lt;Route path=&quot;/path2&quot; element={&lt;AnotherComponent data={data} /&gt;} /&gt; 
    &lt;/Routes&gt;
  );
}

Make sure you aren't accidentally rendering a self-closed Router component tag, it should wrap all the routes and UI content that needs to access the routing context it provides.

import { BrowserRouter as Router, Routes, Route } from &#39;react-router-dom&#39;;

function App() {
  return (
    &lt;Router&gt;
      &lt;SeparateRoutes /&gt;
      &lt;Routes&gt;
        &lt;Route path=&quot;/&quot; element={&lt;SomeComponent /&gt;} /&gt;
        &lt;Route path=&quot;/path&quot; element={&lt;AnotherComponent /&gt;} /&gt;
      &lt;/Routes&gt;
    &lt;/Router&gt;
  );
}

If you only want data to be fetched for specific routes you might also try creating a layout route and rendering nested routes instead of an entirely separate component with descendent routes.

Example:

import { Outlet } from &#39;react-router-dom&#39;;

function DataLayout() {
  const [data, setData] = useState([]);

  useEffect(() =&gt; {
    ...
    setData([1, 2, 3]);
  }, []);

  // Pass data state in Outlet context
  return &lt;Outlet context={data} /&gt;;
}
import { BrowserRouter as Router, Routes, Route } from &#39;react-router-dom&#39;;

function App() {
  return (
    &lt;Router&gt;
      &lt;Routes&gt;
        &lt;Route path=&quot;/&quot; element={&lt;SomeComponent /&gt;} /&gt;
        &lt;Route path=&quot;/path&quot; element={&lt;AnotherComponent /&gt;} /&gt;
        &lt;Route element={&lt;DataLayout /&gt;}&gt;
          &lt;Route path=&quot;/path1&quot; element={&lt;SomeComponent /&gt;} /&gt;
          &lt;Route path=&quot;/path2&quot; element={&lt;AnotherComponent /&gt;} /&gt; 
        &lt;/Route&gt;
      &lt;/Routes&gt;
    &lt;/Router&gt;
  );
}
import { useOutletContext } from &#39;react-router-dom&#39;;

export const AnotherComponent = () =&gt; {
  const data = useOutletContext();

  ...
};

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

发表评论

匿名网友

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

确定