如何在ReactJS中将数据从子组件传递给其父组件?我试图将数据从子组件发送到其父组件,如下所示: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有人能帮我找出问题吗?子组件正在从一个json文件中创建一个下拉列表,我需要下拉列表来显示JSON数组的两个元素(比如:“AAA,English”作为首选!){
"languages":[
[
"aaa",
"english"
],
[
"aab",
"swedish"
],}
3 回答
海绵宝宝撒
TA贡献1809条经验 获得超8个赞
valueField
textField
DropdownList
简短回答
家长:
<div className="col-sm-9" > <SelectLanguage onSelectLanguage={this.handleLanguage}/> </div>
儿童:
handleLangChange = () => { var lang = this.dropdown.value; this.props.onSelectLanguage(lang); }
详细说明:
编辑:
React.Component
class ParentComponent extends React.Component{ state: { language: '' } handleLanguage = (langValue) => { this.setState({language: langValue}); } render() { return ( <div className="col-sm-9" > <SelectLanguage onSelectLanguage={this.handleLanguage}/> </div> ) }}
var json = require("json!../languages.json");var jsonArray = json.languages;export class SelectLanguage extends React.Component { state = { selectedCode: '', selectedLanguage: jsonArray[0], } handleLangChange = () => { var lang = this.dropdown.value; this.props.onSelectLanguage(lang); } render() { return ( <div > <DropdownList ref={(ref) => this.dropdown = ref} data={jsonArray} valueField='lang' textField='lang' caseSensitive={false} minLength={3} filter='contains' onChange={this.handleLangChange} /> </div> ); }}
createClass
const ParentComponent = React.createClass({ getInitialState() { return { language: '', }; }, handleLanguage: function(langValue) { this.setState({language: langValue}); }, render() { return ( <div className="col-sm-9" > <SelectLanguage onSelectLanguage={this.handleLanguage}/> </div> );});
儿童
var json = require("json!../languages.json");var jsonArray = json.languages;export const SelectLanguage = React.createClass({ getInitialState: function(){ return{ selectedCode: '', selectedLanguage: jsonArray[0], }; }, handleLangChange: function () { var lang = this.refs.dropdown.value; this.props.onSelectLanguage(lang); }, render() { return ( <div > <DropdownList ref='dropdown' data={jsonArray} valueField='lang' textField='lang' caseSensitive={false} minLength={3} filter='contains' onChange={this.handleLangChange} /> </div> ); }});
杰森:
{ "languages":[ { "code": "aaa", "lang": "english" }, { "code": "aab", "lang": "Swedish" }, ] }
慕莱坞森
TA贡献1810条经验 获得超4个赞
将数据从子组件传递到父组件
getData(val){ // do not forget to bind getData in constructor console.log(val);}render(){ return(<Child sendData={this.getData}/>);}
demoMethod(){ this.props.sendData(value); }
白衣非少年
TA贡献1155条经验 获得超0个赞
class ParentComponent extends Component{ onSubmit(data) { let mapPoint = this.getMapPoint(); } render(){ return ( <form onSubmit={this.onSubmit.bind(this)}> <ChildComponent getCurrentPoint={getMapPoint => {this.getMapPoint = getMapPoint}} /> <input type="submit" value="Submit" /> </form> ) }}
class ChildComponent extends Component{ constructor(props){ super(props); if (props.getCurrentPoint){ props.getCurrentPoint(this.getMapPoint.bind(this)); } } getMapPoint(){ return this.Point; }}
添加回答
举报
0/150
提交
取消