0

I may have missed something in my concept, I am updating my component value onClick of button and passing updated value to parent component. but my parent component running behind then child component. can any one please let me know what I am missing?

here is my parent component

onClick = (key) =>{
    console.log("key",key)
}
 <Volume onClick={this.onClick.bind(this)} volume={data}/>

Here is my volumeComponent

constructor(props){
       super(props);
       this.state ={
           volume: this.props.volume
       }
   }
    updateValue(type){
        console.log(type)
        this.setState({
            volume:type == "Inc"? this.state.volume+0.5:this.state.volume-0.5
        })

        this.props.onClick(this.state.volume);
    }
    render(){
        return(
            <div>
             <button type="button" onClick = {this.updateValue.bind(this,"Inc")}>
                                    <span className="glyphicon glyphicon-arrow-up"/>
                                    </button>
                                    <span className="value-growth"> 
                 {(Math.round(this.state.volume*10)/10).toFixed(1)}
              </span>
                                         <button disabled={this.state.volume<=0} type="button"

                                     onClick = {this.updateValue.bind(this,"Dec") } >
                                    <span className="glyphicon glyphicon-arrow-down"/>
                                    </button>
                                    </div>
        )
    }
}

now when I see in logs i.e my UI is showing value 1.1 but my logs showing value 0.6. same thing happens if I click down button. logs shows 1.1 value while UI 0.6 please help me put here

LowCool
  • 1,187
  • 5
  • 25
  • 51
  • here what i can suggest you that you should call you all logic in parent component handle you state in the parent component . that will help you to run you logic correctly . and pass you components state to your child suing props . try to implement this will help you a lot – HpDev Aug 09 '17 at 17:19
  • Have a loop at the duplicate question, it happens because of the async nature of setState – Shubham Khatri Aug 09 '17 at 17:19
  • yes that also make a point – HpDev Aug 09 '17 at 17:20
  • anyone please can explain with me example how should I do? – LowCool Aug 09 '17 at 17:21

0 Answers0