React Native不显示正确的评级星

时间:2018-05-27 15:48:41

标签: android reactjs react-native ratingbar

我不知道为什么,但即使我已经调用了这个方法,我的评级星也没有更新。这是我的示例代码:

import { Rating } from 'react-native-elements-master';

componentDidMount() {

return this.mediavoti()

}

我的costructor:

  constructor(props) {

super(props);

this.state = {

  Mediaevento: '2'
}
}

这是我的方法:

mediavoti() {   

  return fetch('url', 
{
method: 'POST',
headers: {
  'Accept': 'application/json',
  'Content-Type': 'application/json',
},
body: JSON.stringify({

  Idevento: this.props.navigation.state.params.TiPasso_Idevento,

})

}).then((response) => response.json())
.then((responseJson) => {
  this.setState({

    Mediaevento: responseJson[0].Media

  }, function () {

  });

}).catch((error) => {
  console.error(error);
});
 }

这是我的评级渲染组件:

 <Rating

      type="star"
      fractions={1}

      startingValue={parseFloat(this.state.Mediaevento)}
      imageSize={40}
      onFinishRating={this.voto}
       showRating
      style={{ paddingVertical: 10 }} />

我不知道为什么但是它总是看到state.mediaevento值是2.但是如果我在mediavoti()方法中使用console.log,我可以看到状态已经改变。

1 个答案:

答案 0 :(得分:0)

你能试试吗? componentWillMount() { return this.mediavoti() }

而不是componentDidMount()