2016-11-27 27 views
0

My appにはアイテムのリストをレンダリングするリストビューがあります。親コンポーネント内の親コンポーネントの子コンポーネントの値(ListView内)へのアクセス方法ネイティブ

Screenshot

私は私の親コンポーネントでその値にアクセスする方法を、2つの項目を選択した上で、私の子コンポーネントのすべてのクリックは、状態値の変化画像で

{ selected: !this.state.selected } 

をトリガ?例えば

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> 
    ) 
    } 
} 

これは私の子コンポーネントである、@freesoulからの要請を受け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> 
    ) 
    } 
} 

は、ここにあります子供のインスタンス(私は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> 
    ) 
    } 
} 

はそれを願っていますhelp

+0

私は恐らくrowStates = {}は無効です、ネイティブに反応してランタイム中にエラーについて不平を言います。州をnullにすることはできません。 –

+0

答えがありがとう、それは2つの間違いがあるが、非常に有用だった。 rowStates:{}とrenderRowのonPressメソッドの内部では、太い矢印の関数onPress = {()=> rowUpdated(item)}を返す必要があります。そうしないと、アプリケーションは自動的にメソッドを実行します –

+1

Uh、ごめんなさい!書く時に気づかなかった。私は間違いを訂正する答えを編集しました。 – martinarroyo

関連する問題