React native inter-component communication local refresh

Source: Internet
Author: User

Component is one of the greatest advantages of react-native, because we can do partial refreshes to improve performance.

The parent component, as shown in the figure, contains a child component addition that can dynamically add subcomponents



Log output:


Add key code:

_onpress () {
        if (show) {/* Display cannot add */
           return when deleted;
        }
        Let arr = this.state.list;
        Arr.push (This._renderitem ());
        This.setstate ({
            list:arr,
        });
    };
    _renderitem () {
        count = this.id+1;
        This.id = count;
        Return (<ceilitem ref={(component) =>{
                This.reflist.push (component)
            }}
                          Id={count}
                          Onchildhanlder={this.onchildhanlder}
            >
            </CeilItem>
        );
    }


Click Show to dynamically update subcomponents without refreshing the parent component

As shown below:


Log


Only the child view is refreshed without refreshing the parent view

Show Key code:

Parent component:

  /* Change the item layout *
    /_show () {
        show = Show?false:true;
        for (let i=0;i<this.reflist.length;i++) {
            this.reflist[i].change (show);
        }
    }
    Onchildhanlder (inputID) {let
        len = this.reflist.length;
        for (let i=0;i<len;i++) {let
            id=this.reflist[i].props.id;
            if (inputid==id) {
                del++;
                Console.log ("Delete I:", i);
                This.reflist[i].delete ();
            }
        }
        /* All deleted before you can add */
        show = Del==count?false:show;
    }

Sub-component code:

  Change (flag) {this.setstate ({flag:flag});
    } delete () {this.setstate ({delete:true});
        } hide () {Let centerpart_right = 0;
        Let rightpart_width = 0; Return (<view style={styles. item}> <view style={styles.leftpart}> </View> <view style={[styles.c Enterpart, {marginright:centerpart_right}]}> </View> <view style={[styles.rightpart,{w
    Idth:rightpart_width,position: ' absolute ', right:0}]}> </View> </View>);
        } show () {Let id=this.props.id;
        Let Onchildhanlder=this.props.onchildhanlder;
        Let centerpart_right = 70;
        Let rightpart_width = 60; Return (<view style={styles. item}> <view style={styles.leftpart}> </View> <view style={[styles.c
      Enterpart, {marginright:centerpart_right}]}> </View>      <view style={[styles.rightpart,{width:rightpart_width,position: ' Absolute ', right:0},{justifycontent: ' Center ',
                    Alignitems: ' Center '}]}> <touchableopacity onpress={() =>{onchildhanlder (ID)}}>
                <text style={[styles.textstyle]}> del </Text>
    </TouchableOpacity> </View> </View>); }

GitHub Full code: Https://github.com/wuyunqiang/react-/tree/master/test/view


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.