我有一个问题。我想从一个组件设置另一个组件的状态,但我不知道该怎么做?
最佳答案
Component1: The component that you want to change the state from that.
Component2: The component that you want to change its state from another component.
- 如果 Component2 是 Component1 的子组件,您可以通过以下方式执行此操作 将新状态作为 props 传递给 Component2。
- 如果 Component2 是 Component1 的父组件,则应通过以下方式执行此操作 Component2 中定义的函数(此函数更改 Component2 的状态)并作为 prop 传递给 Component1。
- 否则,您可以使用
redux
来管理状态并定义一个 Component2 中的mapStateToProps
函数通过更改 由 Component1 表示。
关于reactjs - 如何从一个组件集中声明 native react 中的另一个组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46629853/