我将如何构建必须从模态接收数据的反应组件
How would I structure react components that must receive data from a modal
现在我的 React 容器看起来像这样:
class TableData extends Component
{
some React Functions and state changes.
<Sidebar passdata as props/>
}
在 Sidebar 内部我有一个模式,它需要更新 Sidebar 组件和 TableData 组件的状态。
我不知道你是否可以在 React 中将 props 传递给父组件,但我知道不推荐这样做。在这些类型的问题中推荐的行动方案是什么?
你是正确的,你不能将 props 传递给父组件。但是,父组件可以将回调作为 prop 传递给它的子组件,当值更改时调用它。
class TableData extends React.Component {
constructor(props) {
// ...
this.state = {
name: originalState,
};
}
handleStateChange(newState) {
this.setState({ name: newState });
}
render() {
// ...
<Sidebar onStateChange={this.handleStateChange.bind(this)} />
// ...
}
}
class Sidebar extends React.Component {
// ...
this.props.onStateChange(newState);
// ...
}
现在我的 React 容器看起来像这样:
class TableData extends Component
{
some React Functions and state changes.
<Sidebar passdata as props/>
}
在 Sidebar 内部我有一个模式,它需要更新 Sidebar 组件和 TableData 组件的状态。
我不知道你是否可以在 React 中将 props 传递给父组件,但我知道不推荐这样做。在这些类型的问题中推荐的行动方案是什么?
你是正确的,你不能将 props 传递给父组件。但是,父组件可以将回调作为 prop 传递给它的子组件,当值更改时调用它。
class TableData extends React.Component {
constructor(props) {
// ...
this.state = {
name: originalState,
};
}
handleStateChange(newState) {
this.setState({ name: newState });
}
render() {
// ...
<Sidebar onStateChange={this.handleStateChange.bind(this)} />
// ...
}
}
class Sidebar extends React.Component {
// ...
this.props.onStateChange(newState);
// ...
}