2

I have a component ParentToDataDisplayingComponent that is creating a few lookups to help format data for a child component based on data in a redux store accessed by the parent of ParentToDataDisplayingComponent.

I am getting some lagging on the components rerendering, where the changing state has not affected this.props.dataOne or this.props.dataTwo - the data in these lookups is guaranteed the same as last render, but the data in props is not guaranteed to be the available (loaded from the backend) when the component mounts. mapPropsToDisplayFormat() is only called after all of the data passed in through the props is available.

I would like to declare the lookup variables once, and avoid re-keyBy()ing on every re-render.

Is there a way to do this inside the ParentToDataDisplayingComponent component?

export default class ParentToDataDisplayingComponent extends Component {

  ...     

  mapPropsToDisplayFormat() {
    const lookupOne = _(this.props.dataOne).keyBy('someAttr').value();
    const lookupTwo = _(this.props.dataTwo).keyBy('someAttr').value();

    toReturn = this.props.dataThree.map(data => 

      ... // use those lookups to build returnObject

    );

    return toReturn;
  }

  hasAllDataLoaded() {
    const allThere = ... // checks if all data in props is available
    return allThere //true or false
  }

  render() {
    return (
      <div>

        <DataDisplayingComponent
          data={this.hasAllDataLoaded() ? this.mapPropsToDisplayFormat() : "data loading"}
        />
      </div>
    );
  }
}

1 Answer 1

3

Save the result of all data loading to the component's state.

export default class ParentToDataDisplayingComponent extends Component {

  constructor(props) {
    super(props)
    this.state = { data: "data loading" }
  }    

  componentWillReceiveProps(nextProps) {
    // you can check if incoming props contains the data you need.
    if (!this.state.data.length && nextProps.dataLoaded) {
      this.setState({ data: mapPropsToDisplayFormat() })
    }
  }

  ...

  render() {
    return (
      <div>
        <DataDisplayingComponent
          data={this.state.data}
        />
      </div>
    );
  }
}

I think depending on what exactly you're checking for in props to see if your data has finished loading, you may be able to use shouldComponentUpdate to achieve a similar result without saving local state.

export default class ParentToDataDisplayingComponent extends Component {

shouldComponentUpdate(nextProps) {
  return nextProps.hasData !== this.props.hasData
}    

mapPropsToDisplayFormat() {
  ...

  toReturn = data.props.dataThree
    ? "data loading"
    : this.props.dataThree.map(data => ... )

  return toReturn;
}  

  render() {
    return (
      <div>
        <DataDisplayingComponent
          data={this.mapPropsToDisplayFormat()}
        />
      </div>
    );
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Azium, looks like this will do it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.