2016-11-27 91 views
0

我的应用程序有一个列表视图,用于呈现项目列表。如何访问React Native中父组件中的子组件值(在ListView中)

Screenshot

我的子组件的每点击触发状态值

{ selected: !this.state.selected } 

在图像的变化上面我选择了2个个子项目,我怎么可以访问我的父组件自己的价值?

例如,

Big Data: true 
IoT: true 

这是我的父母片段,Hosted on Github too

import InterestItem from './InterestItem'; 

class AddInterest extends Component { 

    componentWillMount() { 
    this.createDataSource(this.props); 
    } 

    componentWillReceiveProps(nextProps) { 
    this.createDataSource(nextProps); 
    } 

    createDataSource({ items }) { 
    const ds = new ListView.DataSource({ 
     rowHasChanged: (r1, r2) => r1 !== r2 
    }); 
    this.dataSource = ds.cloneWithRows(items); 
    } 

    //return arrays of event from events 
    renderRow(item) { 
    return <InterestItem item={item} icon={computer} />; 
    } 

    render() { 
    const { centerEverything, skeleton, container, textContainer, contentContainer, listViewContainer, 
     titleContainer, descContainer, title, desc, submitContainer, submitTitle } = styles; 
    return (
     <View style={[container]}> 
     <View style={[centerEverything, textContainer]}> 
      <View style={titleContainer}> 
      <Text style={[title]}>What kind of events are you interest in?</Text> 
      </View> 
      <View style={descContainer}> 
      <Text style={[desc]}>You'll see more events from the categories you choose.</Text> 
      </View> 
     </View> 

     <View style={[contentContainer]}> 
      <ListView 
      enableEmptySections 
      contentContainerStyle={listViewContainer} 
      dataSource={this.dataSource} 
      renderRow={this.renderRow} 
      /> 
     </View> 

     <View style={[centerEverything, {paddingBottom: 10}]}> 
      <View style={[centerEverything, submitContainer]}> 
      <Text style={submitTitle}>Submit</Text> 
      </View> 
     </View> 

     </View> 
    ) 
    } 
} 

这是我的孩子组成,Hosted on GitHub too

class InterestItem extends Component { 

    state = { 
    selected: false 
    } 

    render() { 
    const { skeleton, centerEverything, container, textStyle } = styles; 
    return(
     <TouchableWithoutFeedback onPress={() => this.setState({ selected: !this.state.selected })}> 
     <View style={[centerEverything, container, { backgroundColor: this.state.selected ? '#635eb4' : '#e7e7e7'}]}> 
      {this.props.icon} 
      <Text style={textStyle}>{this.props.item[0]}</Text> 
     </View> 
     </TouchableWithoutFeedback> 
    ) 
    } 
} 

在从@freesoul要求,这里的儿童实例(考虑到我有8个孩子) enter image description here

+0

考虑一个'InterestItem'实例,可以是'this.props.item'的值是什么?你能举一个例子吗? –

+0

@ free-soul我已经添加了所需的信息 –

+2

我的建议是,你应该保持在你父母的孩子的状态;像'state = {cars:false,bigData:true,hackintosh:false,iot:true,...}'。 –

回答

2

上@自由灵魂的后续行动,我会做如下修改代码:

constructor(){ 
    this.state = { 
    //... 
     rowStates: {} // Holds the state for each row, identified by the uid property (I'm assuming it is unique, otherwise use some other value) 
    } 
    this.renderRow = this.renderRow.bind(this); 
    this.rowUpdated = this.rowUpdated.bind(this); 
} 
renderRow(item) { 
    // Adds a callback so that we know when an element has been pressed 
    return <InterestItem item={item} icon={computer} onPress={() => this.rowUpdated(item)}/>; 
} 

rowUpdated(item){ 
    let rowStates = {...this.state.rowStates}; // Make a copy of the object 
    rowStates[item.uid] = !rowStates[item.uid]; // If the item is not in the object, !undefined will be evaluated, which results in true, so the operation is safe 
    this.setState({rowStates}); 
} 

然后,你的孩子组件应该是这样的:

class InterestItem extends Component { 

    state = { 
    selected: false 
    } 

    render() { 
    const { skeleton, centerEverything, container, textStyle } = styles; 
    return(
     <TouchableWithoutFeedback onPress={() => {this.setState({ selected: !this.state.selected }); if(this.props.onPress) this.props.onPress(this.props.item)}}> 
     <View style={[centerEverything, container, { backgroundColor: this.state.selected ? '#635eb4' : '#e7e7e7'}]}> 
      {this.props.icon} 
      <Text style={textStyle}>{this.props.item[0]}</Text> 
     </View> 
     </TouchableWithoutFeedback> 
    ) 
    } 
} 

希望它帮助

+0

恐怕rowStates = {}是无效的,在运行时反应原生抱怨错误。状态不能为空? –

+0

感谢您的回答,这是非常有帮助的,虽然有2个错误。 rowStates:{}和renderRow中的onPress方法,我们应该返回胖箭头函数,onPress = {()=> rowUpdated(item)}否则应用程序将自动运行该方法 –

+1

呃,对不起!写作时没有注意到。我编辑了答案来纠正错误。 – martinarroyo

相关问题