Skip to content

Instantly share code, notes, and snippets.

@carlows
Created September 19, 2016 21:34
Show Gist options
  • Save carlows/ae8fa97d10944d20a6b42560fd3e312d to your computer and use it in GitHub Desktop.
Save carlows/ae8fa97d10944d20a6b42560fd3e312d to your computer and use it in GitHub Desktop.

Author

Carlos Martinez

Context

To update the timer when started, we are setting an interval that runs each second and updates the state with the new values for the timer.

The problem is that this doing this, is causing the whole Timer component to rerender each second. And because that component acts as a parent for the TimeEntriesList component this rerender will cause performance issues.

Solution

The timer form is currently being rendered like this:

class Timer {
  render() {
    return (
      <div>
        <input />
        <input />
        <input />

        <TimeEntriesList />
      </div>
    );
  }
}

If we extract the form to its own component, when the interval updates the state we won't be updating the state for the sibling components anymore:

class Timer {
  render () {
    return (
      <div>
        <TimerForm />
        <TimeEntryList />
      </div>
    );
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment