N
N
Nikita Shchypylov2018-08-10 10:30:00
React
Nikita Shchypylov, 2018-08-10 10:30:00

What is the correct way to update the component in this case?

Hello!
I implement logics with a ban on the re-rendering of the component if the button is clicked. Now the code is like this:

state = {
  updatesLocked : false
};

shouldComponentUpdate(nextProps, nextState) {
  return !nextState.updatesLocked
}

updatesButtonHandle = () => {
  this.setState({
    updatesLocked : !this.state.updatesLocked
  });
}

render () {
  const { updatesLocked } = this.state;
  return (
    <div className={styles.updatesButton} onClick={this.updatesButtonHandle} role="presentation">
      {updatesLocked ? (
        <div className={styles.lockIcon}>
          <LockClosedIcon />
        </div>
      ) : (
        <div className={styles.lockIcon}>
          <LockOpenIcon />
        </div>
      )}
    </div>
  )
}

As you can see from the code, when I click on the button, I change the state and, based on the state, I disable / allow updating the component, but there is a problem with changing the icon in the button. It doesn't update when you click on it (because shouldComponentUpdate fires).
How can I update the icon on button click?
Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-08-10
@Nikulio

The condition for allowing an update should be a little more complicated - when denied, you must allow an update if you are trying to unlock, i.e.:

shouldComponentUpdate(nextProps, nextState) {
  return !this.state.updatesLocked || !nextState.updatesLocked;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question