React-native Activityindicator initial value is false and cannot be displayed again

Source: Internet
Author: User
Tags bind constructor

Today encountered a very wonderful problem, and later in the StackOverflow above, is said to be a react-native bug.
Original address: http://stackoverflow.com/questions/38579665/ Reactnative-activityindicator-not-showing-when-animating-property-initiate-false

For example, the following code:

Constructor (props) {
Super (props);
This.state = {
Animating:true,
};
}

Test () {

This.setstate (Animationg:. this.state.animating);

}

Render () {

Return

<View>

<button onpress = {This.test.bind (this)}>

</Button>

<activityindicator animating = {this.state.animating}/>

</View>

);

}

In this case (the animating initial value is true), the load box can be displayed and hidden by clicking on the button to achieve the desired effect.

But if the initial value of animating is false, as follows:

Constructor (props) {
Super (props);
This.state = {
Animating:false,
};
}

Test () {

This.setstate (Animationg:. this.state.animating);

}

Render () {

Return

<View>

<button onpress = {This.test.bind (this)}>

</Button>

<activityindicator animating = {this.state.animating}/>

</View>

);

}

The load box cannot be displayed by clicking the button anyway.

Later on React-native official GitHub see a magical solution to this problem, the original address: https://github.com/facebook/react-native/issues/11682

Just add


<activityindicator animating = {this.state.animating}/> changed to {this.state.animating && < Activityindicator animating={this.state.animating}/>}

To run correctly.

The complete code is as follows:

Constructor (props) {
Super (props);
This.state = {
Animating:false,
};
}

Test () {

This.setstate (Animationg:. this.state.animating);

}

Render () {

Return

<View>

<button onpress = {This.test.bind (this)}>

</Button>

{this.state.animating && <activityindicator animating={this.state.animating}/>}

</View>

);

}



<button onpress = {This.test.bind (this)}>

</Button>

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.