我正在尝试将数据从子组件发送到其父组件,如下所示:
const ParentComponent = React.createClass({
getInitialState() {
return {
language: '',
};
},
handleLanguageCode: function(langValue) {
this.setState({language: langValue});
},
render() {
return (
<div className="col-sm-9" >
<SelectLanguage onSelectLanguage={this.handleLanguage}/>
</div>
);
});
这是子组件:
export const SelectLanguage = React.createClass({
getInitialState: function(){
return{
selectedCode: '',
selectedLanguage: '',
};
},
handleLangChange: function (e) {
var lang = this.state.selectedLanguage;
var code = this.state.selectedCode;
this.props.onSelectLanguage({selectedLanguage: lang});
this.props.onSelectLanguage({selectedCode: code});
},
render() {
var json = require("json!../languages.json");
var jsonArray = json.languages;
return (
<div >
<DropdownList ref='dropdown'
data={jsonArray}
value={this.state.selectedLanguage}
caseSensitive={false}
minLength={3}
filter='contains'
onChange={this.handleLangChange} />
</div>
);
}
});
我需要的是由用户在父组件中获取选定的值。我收到此错误:
Uncaught TypeError: this.props.onSelectLanguage is not a function
谁能帮我发现问题?
PS子组件正在从json文件创建一个下拉列表,我需要该下拉列表来显示json数组的两个元素彼此相邻(例如:“ aaa,english”是首选!)
{
"languages":[
[
"aaa",
"english"
],
[
"aab",
"swedish"
],
}
handleLanguageCode: function(langValue) { this.setState({ language: langValue }).bind(this); },
但是它返回一个错误:ncaught TypeError: Cannot read property 'bind' of undefined
createClass
绑定非反应方法。
<SelectLanguage onSelectLanguage={this.handleLanguage*Code*}/>
错字。