J
J
Jasulan982018-10-07 21:26:41
React Native
Jasulan98, 2018-10-07 21:26:41

Flatlist not updating?

There is a Parent component

class Parent extends React.Component {
      constructor(props){
      super(props);
      this.state={data:[]}
      }
     componentDidMount(){
     /*Загружаю данные с сервера*/
   } 
    render(){
     return(
        <Child data={this.state.data} refresh={this.state.refresh}/>
    )
    }
   }

Child Component
class Child extends React.Component {
      constructor(props){
      super(props);
      this.state={refresh:this.props.refresh}
      }
    render(){
     <FlatList  
                   keyExtractor = {( item, index ) => index.id }
                   data = { this.props.data}
                   renderItem={this.renderItem.bind(this)}
                   extraData={this.props.refresh}{/*поставил даже  extraData={this.props},extraData={this.state.refresh} или даже подписался на родительский компонент чтобы обновить не вышло.*/}                   
            />
    )
    }
   }

And when the data in the Parent component changes, the Child does not change. Used for Parent shouldcomponentDidUpdate did not work . In short, how to update the flatlist if in a child component?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nick, 2018-10-17
@DragonSpirit

In theory, you can push this.props.data into extraData and a re-render will be called when it changes. Is the data updated correctly in the child component? You can check in the deprecated componentWillReceiveProps whether new props are coming to it. And in general, how does the update happen in componentDidMount? I hope through this.setState, and not directly?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question