2

I am currently developing a react redux based web application which displays large amount of data on the UI. When the data size increases, the frame per second decreases. Also, certain forms displaying components take longer and appear to be sluggish. If someone could guide me on correct rendering method or some coding standards needed to be followed for such applications, it will be a great help. -Thanks

I am currently checking whether my application uses react lifecycle components (explicitly by any other developer). I am also suspecting the way in which components are rendered.

3
  • Show what you have tried as a minimal reproducible example. SO is a terrible tutorial site. Commented Jan 25, 2019 at 17:07
  • 1
    Immutable data and pure components. Commented Jan 25, 2019 at 17:18
  • Much too broad for a useful answer. Commented Jan 25, 2019 at 17:22

2 Answers 2

1

Hello and welcome to StackOverflow! Your question is very generic, so it's hard to pinpoint exactly how to resolve it.
I guess the first thing I'd do is take a look in chrome's performance tab in the developers tools. You can use it to profile you application and see what functions take the longest.
You can find helpful information here and here.
This will give you a good starting point.
As far as profiling a React application, you can take a look at React's Dev Tool profiler, more info can be found here.

You might also want to make sure to avoid the deprecated lifecycle functions, as they are known to cause performance issues. Those are:

componentWillMount
componentWillRecieveProps
componentWillUpdate

And make sure you perform all HTTP requests after components mount.

If everything fails, you should look into memoization techniques. Memoizing is basically saving the result of a function call in memory, so the next time your function is called with the same arguments, you don't recalculate the output. For that you can use React's builtin memo feature to memoize complete components, and a selector (like reselect) to memoize redux computations.

Sign up to request clarification or add additional context in comments.

4 Comments

Refrain using ComponentWillReceiveProps it will be deprecated in new versions
He literally said "make sure to avoid the deprecated lifecycle functions" @BonAndreOpina.
That's why I specifically wrote to avoid those functions
Thank you @DorShinar for your response. I am currently looking into react dev tool profiler. Is there any other tool available which can help me to check the unnecessary rendering of components .
0

Please Check Prevent Unnecessary Rendering

All children in component re-renders when its props or state gets updated. This is the default behavior, and, since React updates only the relevant DOM nodes, this process is usually fast. However, there are certain cases where the component’s output stays the same regardless of whether the props have changed.

To alter the default implementation, you can use a lifecycle hook called shouldComponentUpdate(). This lifecycle hook doesn’t get invoked during the initial render, but only on subsequent re-renders. shouldComponentUpdate() is triggered when the props have changed and it returns true by default.

shouldComponentUpdate(nextProps, nextState) {
  return true;
}

If you’re sure that your component doesn’t need to re-render regardless of whether the props have updated or not, you can return false to skip the rendering process.

class ListItem extends Component {
    shouldComponentUpdate(nextProps, nextState) {
     return false
    }
    
    render() {
    // Let the new props flow, I am not rendering again.
    }
}

Alternatively, if you need to update only when certain props are updated, you can do something like this:

class ListItem extends Component {
    shouldComponentUpdate(nextProps, nextState) {
        return nextProps.isFavourite != this.props.isFavourite;
    }
    ...
}

For example here, we’re checking whether the current value of isFavourite has changed before each render (nextProps holds the new value of the prop) and if yes, the expression returns true. The component gets re-rendered. The changes in any other props won’t trigger a render() since we’re not comparing them in shouldComponentUpdate().

Attention Replacing ‘componentWillReceiveProps’ with ‘getDerivedStateFromProps’

With the release of React 16.3, some new lifecycle methods have been introduced, and release of React 17 will deprecate some lifecycle method.

You can find helpful information here

Comments

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.