Javascript——子项下的状态更改不在父项中持久化
嗨(:我一直在尝试使用一个子-父通信系统,以确保我在子系统中所做的更改显示在父系统中。更具体地说,在我的React应用程序的一个页面中,我希望有一些“切换”对象,我希望能够打开/关闭这些对象,并使这些更改在不同的页面中保持不变。这些是我遵循的原则是:Javascript——子项下的状态更改不在父项中持久化,javascript,reactjs,parent-child,Javascript,Reactjs,Parent Child,嗨(:我一直在尝试使用一个子-父通信系统,以确保我在子系统中所做的更改显示在父系统中。更具体地说,在我的React应用程序的一个页面中,我希望有一些“切换”对象,我希望能够打开/关闭这些对象,并使这些更改在不同的页面中保持不变。这些是我遵循的原则是: 在我的父级中创建状态: class dashboard extends Component { constructor(props) { super(props); this.updateObject.bind(this);
class dashboard extends Component {
constructor(props) {
super(props);
this.updateObject.bind(this);
}
state = {
test_dictionary: {'test': false}
};
updateObject(current_key) {
var new_state = Object.assign({}, this.state.test_dictionary);
new_state[current_key] = !new_state[current_key]; // I want to flip the boolean when clicked
this.setState({test_dictionary: new_state});
}
<childView test_dictionary={this.state.test_dictionary} updateObject={this.updateObject}/>
<Switch
checked={this.props.test_dictionary['test']}
onChange={this.props.updateObject.bind('test')}
/>
Uncaught TypeError: Cannot read property 'updateObject' of undefined
at String.handleSelectionChange (childView?9911:28)
at String.handleSelectionChange (createPrototypeProxy.js?9d62b87:44)
at Switch.setChecked (Switch.js?bc7e1ee:54)
at toggle (Switch.js?bc7e1ee:105)
at Object.ReactErrorUtils.invokeGuardedCallback (ReactErrorUtils.js?c23a69d:69)
at executeDispatch (EventPluginUtils.js?46a03e1:85)
at Object.executeDispatchesInOrder (EventPluginUtils.js?46a03e1:105)
at executeDispatchesAndRelease (EventPluginHub.js?2c8fe93:43)
at executeDispatchesAndReleaseTopLevel (EventPluginHub.js?2c8fe93:54)
at Array.forEach (<anonymous>)
Uncaught TypeError:无法读取未定义的属性'updateObject'
at String.handleSelectionChange(儿童视图?9911:28)
在String.handleSelectionChange(createPrototypeProxy.js?9d62b87:44)
在Switch.setChecked(Switch.js?bc7e1ee:54)
at切换(Switch.js?bc7e1ee:105)
位于Object.ReactErrorUtils.invokeGuardedCallback(ReactErrorUtils.js?c23a69d:69)
在executeDispatch(EventPluginUtils.js?46a03e1:85)
在Object.executeDispatchesInoder(EventPluginUtils.js?46a03e1:105)
在执行的DispatchesAndRelease(EventPluginHub.js?2c8fe93:43)
在executeDispatchesAndReleaseTopLevel(EventPluginHub.js?2c8fe93:54)
在Array.forEach()处
如果您能提供任何帮助,我将不胜感激!我不能100%确定这是原因,但这可能是因为
bind
返回一个新函数,它的this
上下文设置为您传递它的任何参数。因此,当您执行此操作时:
<Switch
checked={this.props.test_dictionary['test']}
onChange={this.props.updateObject.bind('test')}
/>
注意
onChange
中的arrow函数。这样我们可以创建一个新函数来正确调用回调属性,但是可以传递我们想要的任何参数。我没有足够的声誉来评论,但是在你完成jered所说的操作之后,你会得到类型错误:无法读取未定义的属性“test\u dictionary”
我会尝试将updateObject
更改为箭头函数,因为当它不是箭头函数时,此
不一定指您正在考虑的此
尝试将函数更改为:
updateObject = (current_key) => {
var new_state = Object.assign({}, this.state.test_dictionary);
new_state[current_key] = !new_state[current_key]; // I want to flip the boolean when clicked
this.setState({test_dictionary: new_state});
}
您似乎将状态放在了一个非常奇怪的位置。这会导致它总是以未定义结束,因为它不在组件的实际范围内
class dashboard extends Component {
constructor(props) {
super(props);
this.state = {
test_dictonary: { test: false }
};
this.updateObject.bind(this);
}
updateObject(current_key) {
const new_key = !this.state.test_dictonary[current_key];
this.setState({test_dictionary: new_key});
}
render() {
return (
<childView test_dictionary={this.state.test_dictionary} updateObject={this.updateObject}/>
)
}
}
类仪表板扩展组件{
建造师(道具){
超级(道具);
此.state={
测试命令:{test:false}
};
this.updateObject.bind(this);
}
updateObject(当前_键){
const new_key=!this.state.test_dictional[当前_key];
this.setState({test_dictionary:new_key});
}
render(){
返回(
)
}
}
在开关上执行此操作以防止发生有趣的绑定,这是您最初错误的根源
<Switch
checked={this.props.test_dictionary['test']}}
onChange={() => { this.props.updateObject('test'); }}
/>
{this.props.updateObject('test');}
/>
非常感谢您的反馈!我将其更改为您建议的,现在的错误是:未捕获类型错误:无法读取未定义的
的属性“test\u dictionary”,而不是未捕获类型错误:无法读取未定义的
的属性“updateObject”。由此,我假设在我的updateObject
中>函数,它无法访问我正在尝试访问的this.state.test\u字典?您有任何进一步的建议吗?我非常感谢您的帮助!
<Switch
checked={this.props.test_dictionary['test']}}
onChange={() => { this.props.updateObject('test'); }}
/>