非常新的 react 。你可以说我还没有开始像 React 那样思考。 问题是这样的:
<div>
<DropDown> </DropDown>
<Panel> </Panel>
</div>
在下拉列表中,我选择一个值。将其存储在状态中,如 currentLocation。 然后我进入面板,点击一个按钮,我想打开一个模式。当我打开模态时,我需要将 currentLocation 传递给该模型。 我可以将任意值传递给模态,但我无法找到从 DropDown 获取当前所选项目的方法。
如何将当前所选项目的值获取到面板? 我说得有道理吗?
最佳答案
当您在下拉列表中调用 setState 时,将强制更新页面。
然后,如果您在组件中调用 this.state
,您应该会获得所需的值。
您应该阅读基本教程以掌握 React 基础知识。
但事情是这样的:
getInitialState: function() {
return {
myVar: ''
//set your variables here
//getInitialState will get called before the component gets mounted
};
},
myCustomFunction: function(newVariable) {
this.setState({
myVar: newVariable
});
},
render: function() {
return (
<div>
<input
onChange={this.myCustomFunction}
/>
<MyCustomComponent myProp={this.state.myVar}/>
//everytime the state changes MyCustomComponent will have that new value as a prop
</div>
);
}
关于javascript - React.JS,组件之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35664312/