简体   繁体   English

在兄弟组件之间传递状态React Router v4

[英]Passing state between sibling components React Router v4

I want to pass state between various sibling components: Dashboard and all routes which contain /overall . 我想在同级组件之间传递状态: Dashboard和所有包含/overall路由。 Thea idea is that I can click a button on any of the pages which contain /overall in the URL and it gets the corresponding value and moves that value to the dashboard. 一个想法是,我可以在URL中包含/overall任何页面上单击一个按钮,它会获取相应的值并将该值移至仪表板。 I've been doing some reading around this subject however I'm struggling to understand which is the best implementation for my purpose. 我一直在阅读有关该主题的文章,但是我一直在努力了解哪种方法最适合我的目的。

index.js index.js

import React from "react";
import { render } from "react-dom";
import Router from "./components/Router";
import registerServiceWorker from "./registerServiceWorker";
import "./css/style.css";
render(<Router />, document.getElementById("main"));
registerServiceWorker();

Router.js Router.js

import React from "react";
import { BrowserRouter, Route, Switch } from "react-router-dom";
import NameSelector from "./NameSelector";
import Dashboard from "./Dashboard";
import OverallGoals from "./OverallGoals";
import OverallShots from "./OverallShots";
import OverallPossession from "./OverallPossession";
import OverallResults from "./OverallResults";
import OverallFormations from "./OverallFormations";
const Router = () => (
  <BrowserRouter>
    <Switch>
      <Route exact path="/" component={NameSelector} />
      <Route exact path="/:gamerId/dashboard" component={Dashboard} />
      <Route exact path="/:gamerId/overall/goals" component={OverallGoals} />
      <Route exact path="/:gamerId/overall/shots" component={OverallShots} />
      <Route
        exact
        path="/:gamerId/overall/results"
        component={OverallResults}
      />
      <Route
        exact
        path="/:gamerId/overall/possession"
        component={OverallPossession}
      />
      <Route
        exact
        path="/:gamerId/overall/formations"
        component={OverallFormations}
      />
      <Route component={NotFound} />
    </Switch>
  </BrowserRouter>
);

export default Router;

From my understanding, passing state through the Route wouldn't work as Route doesn't actually render anything. 据我了解,通过Route传递状态是行不通的,因为Route实际上并没有渲染任何东西。 I need a parent component to hold state for both siblings however it doesn't make sense to make a component solely for managing state between the two components (or would it?). 我需要一个父组件来保持两个兄弟的状态,但是仅使一个组件用于管理两个组件之间的状态没有意义(或者会吗?)。

What's the best practice for this and how would it fit into my codebase? 最佳做法是什么,它如何适合我的代码库?

Also, I want to avoid Redux for now, as I will appreciate the technology more when I implement this solution in pure React. 另外,我现在想避免使用Redux,因为当我在纯React中实现该解决方案时,我会更加欣赏这项技术。

Fairly new to React, I understand this code could be slightly more efficient. 对React来说还算是新手,我知道这段代码可能会更高效。

EDIT: NameSelector is a component which holds an input field which once submitted push() es to a URL containing the input 编辑: NameSelector是一个包含输入字段的组件,该字段一旦提交push()到包含输入的URL

Regards, James. 问候,詹姆斯。

I need a parent component to hold state for both siblings however it doesn't make sense to make a component solely for managing state between the two components (or would it?). 我需要一个父组件来保持两个兄弟的状态,但是仅使一个组件用于管理两个组件之间的状态没有意义(或者会吗?)。

It does! 是的!

Pretty much, short of using Redux or the React Context API , you are going to need a common parent component that holds, shares, and manages updates to the state that you'd like your two sibling components to share. 几乎不需要使用Redux或React Context API ,您将需要一个通用的父组件来保存,共享和管理对您希望两个兄弟组件共享的状态的更新。

If your component-tree is simple and short, and the sibling components are in close proximity (ie you don't need to pass the state up-and-down multiple tiers), then this configuration makes a lot of sense. 如果您的组件树简单而又短,并且兄弟组件非常接近(即,您不需要上下传递状态多层),那么这种配置就很有意义。 Have a parent component that manages state, render the two siblings underneath it. 有一个管理状态的父级组件,在其下呈现两个同级。

If the tree structure is (or will become) complex, with components and subcomponents separated from each other across multiple tiers, then start thinking about and investing in a Redux store. 如果树结构很复杂(或将变得很复杂),并且组件和子组件在多层之间相互分离,则开始考虑并投资Redux商店。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM