【发布时间】:2023-03-08 13:44:01
【问题描述】:
在container 组件中,如何在基于this.props 的calculateState 静态方法中从存储中获取状态?
【问题讨论】:
标签: reactjs reactjs-flux flux
在container 组件中,如何在基于this.props 的calculateState 静态方法中从存储中获取状态?
【问题讨论】:
标签: reactjs reactjs-flux flux
容器无法访问 props 默认情况下容器无法访问任何 props。这既是出于性能原因,也是为了确保容器是可重用的,并且 props 不必在整个组件树中进行线程化。在某些有效情况下,您需要根据道具和商店的状态来确定您的状态。在这些情况下,将选项 {withProps: true} 作为第二个参数传递给 create()。这会将组件 props 作为 calculateState() 的第二个参数公开。
class CounterContainer extends Component {
static calculateState(prevState,props) {
return {
counter: CounterStore.getState(props.id),
};
}
render() {
return <CounterUI counter={this.state.counter} />;
}
}
const container = Container.create(CounterContainer, {withProps:true});
【讨论】:
getStateById方法,问题是我无法访问this.props.params.id里面的this.props.params.id例如静态方法。
self。
self(语法方法:unexpected token)。