The method (recommended) for the child component to call the parent component under ES6, and the method for calling the es6 component

Source: Internet
Author: User

The method (recommended) for the child component to call the parent component under ES6, and the method for calling the es6 component

For some purposes, I recently started to study RN again. Looking at the tutorial step by step, I recently encountered a problem, that is, the method call of the Parent and Child components.

I have been Baidu for a long time. Under the ES6 Syntax of JS, I used class to create components, and the subcomponent continuously reported errors when calling the parent component method simulator.

Because the video I watched is implemented based on the es5 syntax, the syntax is somewhat different.

In es5 syntax, this method has been handled by RN, so the results can be achieved according to the example in the video, but es6 seems to have to be written by itself ..

Add this. xxxxx = this. xxxxx. bind (this) in constructor );

Or write this. xxxxx. bind (this ).

I will not talk about it more here. The code below is for reference by people who need it.

Export default class TestPrj extends Component {constructor (props) {super (props); this. timesReset = this. timesReset. bind (this); this. state = {timex: 2 };} timesReset () {this. setState ({timex: 0});} render () {return (<View style = {styles. container}> <Son ref = "Son1" timex = {this. state. timex} timesReset = {this. timesReset}/> // or <Son ref = "Son1" timex = {this. state. timex} timesReset = {this. timesReset. bind (t His)}/> </View>);} class Son extends Component {constructor (props) {super (props); this. state = {times: this. props. timex };} componentWillReceiveProps (props) {console. log (this. props); this. setState ({times: props. timex})} timesReset () {this. props. timesReset ();} render () {return (<View style = {styles. container }> <Text style = {styles. instructions}> son: although you beat me {this. state. times} times, but I will never go wrong !! </Text> <TouchableHighlight style = {styles. btn} underlayColor = {'pink '} onPress = {this. timesReset. bind (this) }>< Text style ={{ textAlign: 'center' }}> hey, give your son another chance !! </Text> </TouchableHighlight> </View> );}}

The method (recommended) for calling the parent component of the sub-component in ES6 is all the content shared by Alibaba Cloud. I hope you can give us a reference and support for the customer's home.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.