简体   繁体   English

交流儿童父母的ReactJs

[英]Communication child-parent ReactJs

I'm learning react js and now I have a situation that I don't know how to resolve and maybe some of you could help me out. 我正在学习react js,现在遇到一种情况,我不知道如何解决,也许有些人可以帮助我。

In my webapplication I have a react component that represents a dropdown to select the language, as follows: 在我的Web应用程序中,我有一个react组件,它代表一个下拉菜单以选择语言,如下所示:

class LocaleSwitcher extends React.Component{


constructor() {
    super();
    this.render = this.render.bind(this);
    this.componentDidMount=this.componentDidMount.bind(this);
    this.state = { languages:[] };
}


render(){


    return (<li>{this.props.selectedLanguage}
        <ul className="dropdown">
            {
                this.state.languages.map(function(result){

                    return (<ListItemWrapper key={result.id}  title={result.text} url="language" />);
                })

            }
        </ul>
    </li>);

}

componentDidMount(){
    var component = this;
    $.get('data/languages',function(result){
        component.setState({languages: result});
    });
}

};

As you can see I'm displaying the selectedLanguage (by default "english") using props : {this.props.selectedLanguage} 如您所见,我正在使用props显示{select.Language(默认为“英语”):{this.props.selectedLanguage}

For the li elements I have created another component ListItemWrapper, and the communication parent-child I'm doing it via props: 对于li元素,我创建了另一个组件ListItemWrapper,我通过props来实现通信的父子过程:

class ListItemWrapper extends React.Component{

constructor() {
super();
this.render = this.render.bind(this);
this.handleClick =this.handleClick .bind(this);
}


 render () {
    console.log("title:" + this.props.title);
    return (<li onClick={this.handleClick}><a href="#">{this.props.title}</a></li>);
}

handleClick () {
    $.get(this.props.url+"?code="+this.props.title,function(result){


/*Insert the code here */

    });
}
};

My problem now is that I don't know how to do the communication from child to parent, as once the user selects a language I would like to update the dropdown with the selected language, so what I need is to fill the method handleClick to send to the parent component the selected language and update it, but I don't know how to do it. 我现在的问题是,我不知道如何进行从孩子到父母的交流,因为一旦用户选择了一种语言,我想用所选语言更新下拉列表,因此我需要填写方法handleClick to将选定的语言发送给父组件并进行更新,但是我不知道该怎么做。 So far I have tried that with no luck 到目前为止,我没有运气尝试过

handleClick () {
    $.get(this.props.url+"?code="+this.props.title,function(result){

this.props.selectedLanguage=this.props.title;

});
}
};

Any help will be very appreciated. 任何帮助将不胜感激。

you have to declare the handle click in your LocaleSwitcher component and pass it to the ListItemWrapper component same like the selected language you have passed. 您必须在LocaleSwitcher组件中声明该句柄单击,然后将其传递给ListItemWrapper组件,就像传递的所选语言一样。

you can also pass the events in props. 您还可以通过道具传递事件。

so your LocaleSwitcher component should look like 所以您的LocaleSwitcher组件应该看起来像

 handleClick () {
    $.get(this.props.url+"?code="+this.props.title,function(result){


/*Insert the code here */

    });
}
    render(){


    return (<li>{this.props.selectedLanguage}
        <ul className="dropdown">
            {
                this.state.languages.map(function(result){

                    return (<ListItemWrapper key={result.id}  title={result.text} url="language" handleClick={this.handleClick}/>);
                })

            }
        </ul>
    </li>);

}

and your ListItemWrapper component look like 和您的ListItemWrapper组件看起来像

     render () {
    console.log("title:" + this.props.title);
    return (<li onClick={this.props.handleClick}><a href="#">{this.props.title}</a></li>);
}

First of, move your handleClick method to LocaleSwitcher. 首先,将handleClick方法移动到LocaleSwitcher。

Then in your render method in LocaleSwitcher do: 然后在LocaleSwitcher中的渲染方法中执行以下操作:

render(){
    return (<li>{this.props.selectedLanguage}
        <ul className="dropdown">
            {
                this.state.languages.map(function(result,i){

                    return (<ListItemWrapper key={result.id}  title={result.text} onclick={this.handleClick.bind(this,i)} url="language" />);
                })

            }
        </ul>
    </li>);
}

Notice the bind, and the "i" variable in the map function. 注意map函数中的bind和“ i”变量。

Then your ListItemWrapper should look like this: 然后,您的ListItemWrapper应该如下所示:

class ListItemWrapper extends React.Component{

constructor(props) {
super(props);
this.render = this.render.bind(this);
}


 render () {
    console.log("title:" + this.props.title);
    return (<li onClick={this.props.handleClick}><a href="#">{this.props.title}</a></li>);
}

The official docs has a little article about this: 官方文档中有一篇关于此的文章:

https://facebook.github.io/react/tips/communicate-between-components.html https://facebook.github.io/react/tips/communicate-between-components.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM