setState is not updating DOM









up vote
1
down vote

favorite












I have this component in my react app. This basically update the state after every second. But The value <p>this.state.time</p> is not changing. I print the value of state it is changing after every second.



import React from 'react'

import './style.css'

class List extends React.Component

constructor(props)
super(props)

this.state =
time:420

this.handleCountdown = this.handleCountdown.bind(this)


handleCountdown()
console.log('4')
console.log(this.state)
setInterval(()=>
this.setState(time:this.state.time-1)
console.log('here')
console.log(this.state)
,1000)


shouldComponentUpdate(nextProps, nextState)
console.log('1')
if(this.props.start != nextProps.start)
console.log('in')
if(nextProps.start == false)
this.setState(time:420)
console.log('2')
return true

else if(nextProps.start == true)
this.handleCountdown()
console.log('3')
return true

else
return false



render()
console.log(this.state)
return(
<div className="test-clock">
<p>this.state.time</p>
<p className='test-clock-subheading'>this.props.start=='true'?'Min remaining':'Start sudo contest'</p>
</div>
)



export default List


It is child component. I import it to parent and then use it.
Can any body tell why DOM is not updating?




this.forceUpdate() will update the DOM. But why this.setState() is not
updating DOM?











share|improve this question

















  • 1




    This is the reason why you should use the getDerivedStateFromProps lifecycle method.
    – Donny Verduijn
    21 hours ago















up vote
1
down vote

favorite












I have this component in my react app. This basically update the state after every second. But The value <p>this.state.time</p> is not changing. I print the value of state it is changing after every second.



import React from 'react'

import './style.css'

class List extends React.Component

constructor(props)
super(props)

this.state =
time:420

this.handleCountdown = this.handleCountdown.bind(this)


handleCountdown()
console.log('4')
console.log(this.state)
setInterval(()=>
this.setState(time:this.state.time-1)
console.log('here')
console.log(this.state)
,1000)


shouldComponentUpdate(nextProps, nextState)
console.log('1')
if(this.props.start != nextProps.start)
console.log('in')
if(nextProps.start == false)
this.setState(time:420)
console.log('2')
return true

else if(nextProps.start == true)
this.handleCountdown()
console.log('3')
return true

else
return false



render()
console.log(this.state)
return(
<div className="test-clock">
<p>this.state.time</p>
<p className='test-clock-subheading'>this.props.start=='true'?'Min remaining':'Start sudo contest'</p>
</div>
)



export default List


It is child component. I import it to parent and then use it.
Can any body tell why DOM is not updating?




this.forceUpdate() will update the DOM. But why this.setState() is not
updating DOM?











share|improve this question

















  • 1




    This is the reason why you should use the getDerivedStateFromProps lifecycle method.
    – Donny Verduijn
    21 hours ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have this component in my react app. This basically update the state after every second. But The value <p>this.state.time</p> is not changing. I print the value of state it is changing after every second.



import React from 'react'

import './style.css'

class List extends React.Component

constructor(props)
super(props)

this.state =
time:420

this.handleCountdown = this.handleCountdown.bind(this)


handleCountdown()
console.log('4')
console.log(this.state)
setInterval(()=>
this.setState(time:this.state.time-1)
console.log('here')
console.log(this.state)
,1000)


shouldComponentUpdate(nextProps, nextState)
console.log('1')
if(this.props.start != nextProps.start)
console.log('in')
if(nextProps.start == false)
this.setState(time:420)
console.log('2')
return true

else if(nextProps.start == true)
this.handleCountdown()
console.log('3')
return true

else
return false



render()
console.log(this.state)
return(
<div className="test-clock">
<p>this.state.time</p>
<p className='test-clock-subheading'>this.props.start=='true'?'Min remaining':'Start sudo contest'</p>
</div>
)



export default List


It is child component. I import it to parent and then use it.
Can any body tell why DOM is not updating?




this.forceUpdate() will update the DOM. But why this.setState() is not
updating DOM?











share|improve this question













I have this component in my react app. This basically update the state after every second. But The value <p>this.state.time</p> is not changing. I print the value of state it is changing after every second.



import React from 'react'

import './style.css'

class List extends React.Component

constructor(props)
super(props)

this.state =
time:420

this.handleCountdown = this.handleCountdown.bind(this)


handleCountdown()
console.log('4')
console.log(this.state)
setInterval(()=>
this.setState(time:this.state.time-1)
console.log('here')
console.log(this.state)
,1000)


shouldComponentUpdate(nextProps, nextState)
console.log('1')
if(this.props.start != nextProps.start)
console.log('in')
if(nextProps.start == false)
this.setState(time:420)
console.log('2')
return true

else if(nextProps.start == true)
this.handleCountdown()
console.log('3')
return true

else
return false



render()
console.log(this.state)
return(
<div className="test-clock">
<p>this.state.time</p>
<p className='test-clock-subheading'>this.props.start=='true'?'Min remaining':'Start sudo contest'</p>
</div>
)



export default List


It is child component. I import it to parent and then use it.
Can any body tell why DOM is not updating?




this.forceUpdate() will update the DOM. But why this.setState() is not
updating DOM?








javascript reactjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 22 hours ago









Rajan Lagah

554313




554313







  • 1




    This is the reason why you should use the getDerivedStateFromProps lifecycle method.
    – Donny Verduijn
    21 hours ago













  • 1




    This is the reason why you should use the getDerivedStateFromProps lifecycle method.
    – Donny Verduijn
    21 hours ago








1




1




This is the reason why you should use the getDerivedStateFromProps lifecycle method.
– Donny Verduijn
21 hours ago





This is the reason why you should use the getDerivedStateFromProps lifecycle method.
– Donny Verduijn
21 hours ago













1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










You also need to check if your state has changed. Add the following to your shouldComponentUpdate method.



}
else if ( this.state.time !== nextState.time
return true;



As mentioned in the documentation:




Use shouldComponentUpdate() to let React know if a component’s output
is not affected by the current change in state or props.







share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237191%2fsetstate-is-not-updating-dom%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    You also need to check if your state has changed. Add the following to your shouldComponentUpdate method.



    }
    else if ( this.state.time !== nextState.time
    return true;



    As mentioned in the documentation:




    Use shouldComponentUpdate() to let React know if a component’s output
    is not affected by the current change in state or props.







    share|improve this answer
























      up vote
      2
      down vote



      accepted










      You also need to check if your state has changed. Add the following to your shouldComponentUpdate method.



      }
      else if ( this.state.time !== nextState.time
      return true;



      As mentioned in the documentation:




      Use shouldComponentUpdate() to let React know if a component’s output
      is not affected by the current change in state or props.







      share|improve this answer






















        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        You also need to check if your state has changed. Add the following to your shouldComponentUpdate method.



        }
        else if ( this.state.time !== nextState.time
        return true;



        As mentioned in the documentation:




        Use shouldComponentUpdate() to let React know if a component’s output
        is not affected by the current change in state or props.







        share|improve this answer












        You also need to check if your state has changed. Add the following to your shouldComponentUpdate method.



        }
        else if ( this.state.time !== nextState.time
        return true;



        As mentioned in the documentation:




        Use shouldComponentUpdate() to let React know if a component’s output
        is not affected by the current change in state or props.








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 21 hours ago









        lumio

        4,31622035




        4,31622035



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237191%2fsetstate-is-not-updating-dom%23new-answer', 'question_page');

            );

            Post as a guest














































































            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto