class QuestionCheck extends Component { constructor(props) { super(props); this.state = { checked: props.checked }; } changeSel = () => { this.props.setCheckd(this.props, !this.state.checked); this.setState({ checked: !this.state.checked, }); }; componentWillReceiveProps(nextProps){ if(this.props.checked !== nextProps.checked){ this.setState({ checked:nextProps.checked }) } } render() { return ( <View style={{ flexDirection: 'row', marginBottom: 10 }} > <CheckBox onPress={() => { this.changeSel(); }} checked={this.state.checked} square={true} color="#DFDFDF" /> <Text style={{ marginLeft: 20 }}>{this.props.name}</Text> </View> ); } }
render() { const baseQuestion1Des = [ { id: '1', name: 'aaa', checked: false }, { id: '2', name: 'bbb', checked: false }, { id: '3', name: 'ccc', checked: false } ]; <Item underline> <Text style={styles.textSize}>类型:</Text> <View style={{ padding: 10, marginTop: 10 }}> <List dataArray={baseQuestion1Des} rowHasChanged={(r1, r2) => { return r1.checked !== r2.checked; }} renderRow={data => { return ( <QuestionCheck setCheckd={this.setCheckd} {...data} /> ); }} /> </View> </Item> }
你好世界!