Skip to content

getDerivedStateFromProps concern #12249

@bradenhs

Description

@bradenhs

Just read about getDerivedStateFromProps and am concerned the new api won't be flexible enough to cover some common use cases.

For instance, I have a scrolling tabs component. Whenever the selectedTabId prop changes I use browser apis to scroll that tab into view. What would be the best way to accomplish this without componentWillReceiveProps?

Currently code is something like this:

class Tabs extends React.Component {
  render() {
    return (
      <div className="tabs-container">
        {this.props.tabs.map(tab => (
          <div className="tab" key={tab.id} id={tab.id}>
            {tab.title}
          </div>
        ))}
      </div>
    );
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.selectedTabId !== this.props.selectedTabId) {
      document.querySelector(`#${nextProps.selectedTabId}`).scrollIntoView();
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions