简体   繁体   English

在反应容器及其子组件之间传递数据

[英]Pass data between a container and its children components in react

I have a <Container /> component that takes a list of <Card /> as children:我有一个<Container />组件,它将<Card />作为子项:

<Container>
  <Card />
  <Card />
</Container>

<Card /> has a boolean state openExpansion that I want once changed, all sibling <Card /> would change to the same value. <Card />有一个 boolean state openExpansion我想要一旦更改,所有兄弟<Card />都会更改为相同的值。 What is the best way to achieve that?实现这一目标的最佳方法是什么? Obviously I can have openExpansion as <Container /> state and inject it to each <Card /> .显然,我可以将openExpansion作为<Container /> state 并将其注入每个<Card /> Also I don't want to use redux store as I don't know where <Container /> will be called.另外我不想使用 redux 商店,因为我不知道<Container />会在哪里被调用。 But is there a way to keep openExpansion as <Card /> state while achieving the same effect?但是有没有办法保持openExpansion as <Card /> state 同时达到同样的效果? Thanks!谢谢!

If you want to share the same value between siblings, you should put openExpansion props in each of the child.如果你想在兄弟姐妹之间共享相同的值,你应该在每个孩子中放置 openExpansion 道具。 Also if you want to change the openExpansion you can create a function in Container此外,如果您想更改 openExpansion,您可以在 Container 中创建 function

class Container extends React.Component{
    ....
    state = {
        openExpansion: true
    }

    changeOpenExpansionValue = (value) => this.setState({openExpansion:value})
    render(
        <div>

        <Card openExpansion={this.state.openExpansion} 
            toggleOpenExpansion={this.changeOpenExpansionValue }/>

        <Card openExpansion={this.state.openExpansion} 
            toggleOpenExpansion={this.changeOpenExpansionValue }/>
        </div>    

    )

}

any time you want to change all the values from one child component just call this.props.toggleOpenExpansion(boolValue) from your Cards任何时候您想更改一个子组件的所有值,只需从您的卡片中调用this.props.toggleOpenExpansion(boolValue)

You can't keep openExpansion value locally in each Card as it would be stored and accessible only inside of Card component and might differ from values in siblings您不能在每个 Card 本地保留 openExpansion 值,因为它只能在 Card 组件内部存储和访问,并且可能与兄弟姐妹中的值不同

Just create a state in the main component and pass the function to update state to Card.只需在主组件中创建一个 state 并传递 function 即可将 state 更新为 Card。 Something like就像是

   const App = () => {

   const [openExpansion, setOpenExpansion] = React.useState(false);

   return (
     <Container>
     <Card setOpenExpansion={setOpenExpansion} openExpansion={openExpansion} />
      <Card />
    <Card setOpenExpansion={setOpenExpansion} openExpansion={openExpansion} /><Card /> /* Similar to above card */
     </Container>
    );
   };

  const Card = ({setOpenExpansion openExpansion})  => {
    return (
       <div onClick={() => openExpansion(true)}>
            {openExpansion ? "opened" : ""}
       </div>
    );
  }

If I'm getting correctly this is something that you want to achieve by passing setState, state variables.如果我说得对,这就是您希望通过传递 setState、state 变量来实现的目标。

I think you explained one option which is having one variable in Container and passing it into every Card component.我认为您解释了一个选项,即在 Container 中有一个变量并将其传递给每个 Card 组件。 Another option is to use a static property inside the Card component.另一种选择是在 Card 组件内使用 static 属性。 Every Card instance would have the same value.每个 Card 实例都将具有相同的值。

It seems to me that you have to lift your state up: https://reactjs.org/docs/lifting-state-up.html在我看来,你必须抬起你的 state: https://reactjs.org/docs/lifting-state-up.html

So in your case, you can create a method/function where you wrap these cards.因此,在您的情况下,您可以创建一个方法/函数来包装这些卡片。

class WrappedCards extends React.Component {
  constructor(props) {
    super(props);
    this.handleOpenExpansion= this.handleOpenExpansion.bind(this);
    this.state = {openExpansion : false};
  }

  handleOpenExpansion(value) {
    this.setState({openExpansion : value});
  }

  render() {

    return (
      <Container>
        <Card
          handleOpenExpansion={this.handleOpenExpansion} />
      </Container>
    );
  }
}

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

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