子组件中有个状态需要在父组件中用到,那么父组件如何能做到实时响应?目前能想到的办法是利用回调,能不能在父组件中直接获取子组件的状态?比如用 this.refs.xxx.state.xxx 这样获取的并不是实时的,能获取实时的吗?像 redux 这样,能做到实时响应,而不需要额外处理?
1 回答
元芳怎么了
TA贡献1798条经验 获得超7个赞
class Parent extends React.Component {
render() { return ( <div>
<Child onChange={this.getChildState} />
</div>
)
}
/** 获取子组件状态 */
getChildState = (state) => {
console.log(state);
}
}
import PropTypes from 'prop-types';
class Child extends React.Component {
static propsType = {
onChange: PropTypes.func
}
// .....
}添加回答
举报
0/150
提交
取消
