How to remove or unmount a component in React Native? -
i want remove component in react native code, "el.parentnode.removechild(el)" in javascript or "[view removefromsuperview]" in objective-c, didn't see related api in react documents. there way make it?
in react native or in react understand don't remove components calling 'remove[..]' changing markup of component changing virtual dom.
here's sample code removes mapview of screen.
'use strict'; var react = require('react-native'); var { appregistry, stylesheet, text, view, switchios, mapview, } = react; var testmap = react.createclass({ getinitialstate() { return { showmap: true, }; }, render: function() { var map = this.state.showmap ? <mapview style={styles.map}/> : null; return ( <view style={styles.container}> <text style={{marginbottom: 10}}>remove view in react native</text> <switchios onvaluechange={(value) => this.setstate({showmap: value})} style={{marginbottom: 10}} value={this.state.showmap} /> {map} </view> ); } }); var styles = stylesheet.create({ container: { flex: 1, margintop: 50, alignitems: 'center', backgroundcolor: '#f5fcff', }, map: { height: 200, width: 300, margin: 10, borderwidth: 1, bordercolor: '#000000', }, }); appregistry.registercomponent('testmap', () => testmap); 
the relevant part being:
render: function() { var map = this.state.showmap ? <mapview style={styles.map}/> : null; return ( <view style={styles.container}> [..] {map} </view> ); }
Comments
Post a Comment