我试图在ReactJS组件中开发两个下拉列表,第二个下拉列表的值取决于第一个下拉列表的值。如果first dropdown值更改,则second dropdown值将基于first dropdown值更改。比如国家和州。以下是代码片段。你能帮我实现一下吗?
谢谢
<script type="text/jsx">
/*** @jsx React.DOM */
var data = [
{key:"eStatements", value:"eStatements"},
{key: "mobileApp", value:"Mobile App"},
{key: "billPay", value:"Bill Pay"},
{key: "remoteDeposit", value:"Remote Deposit"},
{key: "onlineBanking ", value:"Online Banking"},
{key: "P2P", value:"P2P"}
];
var myOptions = [];
var DropdownApp = React.createClass({
render:function(){
return (
<div>
<SelectApp data={this.props.data} name={this.props.name}/>
</div>
);
}
});
var SelectApp = React.createClass({
getInitialState:function(){
return {myOptions : {"signUp" : "Sign Up"}};
},
handleSelectChange : function(e){
var product = e.target.value;
if("eStatements" == product) {
myOptions = [{key: "signUp" , value: "Sign Up"}];
} else if("Mobile App" == product) {
myOptions = [{key: "noOfLogins" , value: "Number of Logins"}];
} else if("Bill Pay" == product) {
myOptions = [{key: "noOfPayments" , value: "Number of Payments"},{key: "addNewPayeeAndPay" , value: "Add New Payee"}];
} else if("Remote Deposit" == product) {
myOptions = [{key: "noOfDeposits" , value: "Number of Deposits"}];
} else if("Online Banking" == product) {
myOptions = [{key: "noOfLogins" , value: "Number of Logins"}];
}else if("P2P" == product) {
myOptions = [{key: "noOfPayments" , value: "Number of Payments"}, {key: "addNewPayeeAndPay" , value: "Add New Payee"}];
}
this.setState({myOptions: myOptions});
},
render: function(){
var opt = this.props.data.map(function(d){
return (<OptionsApp key={d.key} value={d.value}/>);
});
return (
<div>
<div>
<select name={this.props.name} id={this.props.name} onChange={this.handleSelectChange}>
{opt};
</select>
</div>
<div>
<select name={this.state.myOptions} >
{opt};
</select>
</div>
</div>
);
}
});
var OptionsApp = React.createClass({
render : function(){
return (
<option value={this.props.key}> {this.props.value}</option>
);
}
});
React.renderComponent(<DropdownApp data={data} name="my name"/>, document.getElementById("dropDown"))
</script>发布于 2016-03-30 07:01:51
如果两个组件需要通信,这可能是它们应该是兄弟的标志。
如果您将多个组件包装在一个父组件中,您可以结合使用上面的一些策略来促进它们之间的通信。
class ParentComponent extends React.Component {
render() {
return (
<div>
<SiblingA
myProp={this.state.propA}
myFunc={this.siblingAFunc.bind(this)}
/>
<SiblingB
myProp={this.state.propB}
myFunc={this.siblingBFunc.bind(this)}
/>
</div>
);
}
// Define 'siblingAFunc' and 'siblingBFunc' here
}http://andrewhfarmer.com/component-communication/#5-parent-component
https://stackoverflow.com/questions/36214918
复制相似问题