【发布时间】:2017-11-05 04:19:44
【问题描述】:
我正在尝试优雅地重构组件,以便我们可以轻松地将组件用于相同的用例。
我有两个组件,
- NewPurchaseOrder(母公司)
- SelectWarehouse(子组合)
SelectWarehouse 组件会在 NewPurchaseOrder 组件中呈现可用仓库的下拉列表。
父组件有一个状态warehouseId 来跟踪选定的仓库,从我正在执行的子组件更新父组件状态(warehouseId),
父组件
<SelectWarehouse
updateTarget={ ( ghostValue ) => {
this.setState( {
warehouseId: ghostValue // updating the state from child comp. value
}, () => {
console.log( 'yokygoky', this.state.warehouseId )
} )
} }
/>
子组件
handleChangeWarehouse( e ) {
this.setState( {
selectedWarehouse: e.target.value
}, () => {
this.props.updateTarget( e.target.value ) // Updating the parent props
} )
}
现在我要做的是只从父组件发送状态名称,而不是发送函数,然后从子组件更新父组件的状态。 它看起来像这样,
<SelectWarehouse
updateTarget={ warehouseId }
/>
然后从子组件更新warehouseId。
有可能吗?
我已经通过仅发送状态而没有运气来测试它,也无法仅将此上下文与简单状态绑定。
【问题讨论】:
-
你只有一个
SelectWarehouse孩子吗?或者您是否正在准备拥有多个SelectWarehouse孩子的能力,因此您想动态定义状态键?
标签: javascript reactjs jsx