简体   繁体   English

反应 if/else 在 setState 之前返回

[英]React if/else returns before setState

I want to go through Function ScoreCalc then return the final "primeCount" and setState primeScore to that number.我想通过 Function ScoreCalc 然后将最终的“primeCount”和 setState primeScore 返回到该数字。

this.state.animeMax is still empty '' when ScoreCalc() runs in componentWillMount.当 ScoreCalc() 在 componentWillMount 中运行时,this.state.animeMax 仍然是空的 ''。 When I refresh page, primeScore or primeCount changes value.当我刷新页面时, primeScore 或 primeCount 会更改值。 I am guessing it is becuase sometimes "max" state is mounted sometimes not!?我猜这是因为有时“最大”状态是安装的,有时不是!?

If anyone has ideas, please let me know.如果有人有想法,请告诉我。

Thank you!谢谢!

export class Quality extends Component {
    constructor (props) {
        super (props) 
            this.state = {
                primeScore: 0,
                netflixScore: 0,
                huluScore: 0,
                provider:[],
                actionMax:'',
                animeMax:'',
                childrenMax:'',
                comedyMax:'',
                documentaryMax:'',
                horrorMax:'',
                musicalMax:'',
                romanceMax:'',
                scifiMax:'',
                thrillerMax:'',
            }
            this.ScoreCalc = this.ScoreCalc.bind(this);
    }
componentWillMount (){
    axios.get('http://localhost:8001/provider')
        .then(res => this.setState({
            provider: res.data
        },()=>{this.ScoreCalc()}))
    axios.get('http://localhost:8001/provider/actionmax')
        .then(res => this.setState({actionMax: res.data}))
.....10 more axios requests to setState for other "max"....

}
ScoreCalc (){
        let primeCount = 0
            if(this.state.provider[0].primeAction >= this.state.actionMax){
                primeCount += this.props.user.action;
                console.log(primeCount)
            } else {
                primeCount += (this.props.user.action*this.state.provider[0].primeAction/this.state.actionMax)
            }

            if(this.state.provider[0].primeAnime >= this.state.animeMax){
                primeCount += this.props.user.anime;
            } else {
                primeCount += this.props.user.anime*this.state.provider[0].primeAnime/this.state.animeMax;
                console.log(this.state.animeMax) //returns empty ''
                console.log(primeCount) //return infinity
            }

            if(this.state.provider[0].primeChildren >= this.state.childrenMax){
                primeCount += this.props.user.children;
            } else {
                primeCount += this.props.user.children*this.state.provider[0].primeChildren/this.state.childrenMax
            }

         console.log(primeCount); //return infinity
         this.setState({primeScore: primeCount})
}

Looks like you have some typos?看起来你有一些错别字?

In scoreCalc() , you are checking like this:scoreCalc() ,您正在检查如下:

if(this.state.provider[0].primeAction >= this.actionMax){

i think it needs to be:我认为它需要是:

if(this.state.provider[0].primeAction >= this.state.actionMax){

The same for the other if 's ?另一个if是一样的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM