A
A
Alexey1002020-08-06 09:41:26
React
Alexey100, 2020-08-06 09:41:26

React why is the component being rendered?

Hello.

Can you please tell me why this component is rendered every time state.users changes ?
state.users is updated every 5 seconds, and whenever it changes, the showMessage function is executed .
Why does this happen if the component does not use data from the state?
If you comment out users: state.users , then the re-render doesn't happen anymore.

PS: The data in the state does not always change, but only if the users have really changed, I check this in the reducer. But the component still renders

function Message(props) {
  const showMessage = (text) => {
    console.log(text)
    return <span>{text}</span>
  }
  return (
    <div>
      {showMessage(props.message.Text)}
    </div>
  )
}

function mapStateToProps(state) {
  return {
    users: state.users
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2020-08-06
@Aleksey100

Because the state object itself changes and based on this component props - mapStateToProps returns a new object each time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question