在React-native中状态更改后,组件不会重新呈现

时间:2018-06-10 02:44:27

标签: javascript reactjs react-native state

这是代码。当它获得favList的道具我控制输出它显示更改,并在shouldComponentUpdate也显示更新状态但组件不重新渲染。 例如,如果在componentwillupdate中获取新道具后获得Componentwillrecieveprops的最新道具是15,那么如果得到16则更新组件状态,那么我还会在下一个状态中显示16但是它不会重新呈现 这是代码

class RestaurantList extends Component {

constructor(props) {
super(props);
this.state = {
  favRest:null,
  selectedRest: null,
  NearbyRestaurants: null,
  loading: true,
  favRest:this.props.favList
};

}
componentWillMount() {
this.setState({
  SnacksLoading: this.props.SnacksLoading,
  RestaurantLoading: this.props.RestLoading,
  fetchRestaurantsError: this.props.fetchRestaurantsError
})
if (this.props.favList) {
  this.setState({
    favRest: this.props.favList
  })
}
this.props.getNearbyrestaurants(50.8507855, 4.3539655);
this.props.getNearbySnacks(50.8507855, 4.3539655);
}


componentWillReceiveProps(next) {
console.log('[[[[[[[',next)
if (next.favList) {
    console.log('[[[[[[[',next.favList)
    this.setState({ favRest: next.favList });
    console.log('##########',this.state)
  }
if (next.Snacks) {
  this.setState({
    Snacks: next.Snacks,
    SnacksLoading: next.SnacksLoading,
    fetchSnacksError: next.fetchSnacksError
  });
}
if (next.Nearby) {
  this.setState({
    NearbyRestaurants: next.Nearby,
    RestaurantLoading: next.RestaurantLoading,
    fetchRestaurantsError: next.fetchRestaurantsError
  });
}
if (next.fetchRestaurantsError) {
  this.setState({ fetchRestaurantsError: next.fetchRestaurantsError })
}
}

shouldComponentUpdate(nextProps, nextState) {
console.log('TTTTTTTTTTTTTTTT',nextProps,nextState)
return true
}

0 个答案:

没有答案
相关问题