1
votes

I'm having a huge form built with react and material-ui. The form uses two way binding and update state for input changes. changing any input filed cause all components to update (obvious by react developer tools TraceReactUpdate enabled). When it comes to TextField it makes my page rendering very slow. using this.shouldComponentUpdate = PureRenderMixin.shouldComponentUpdate.bind(this) doesn't help.

1
You may be able to tune the render by making this optimisation richardscarrott.co.uk/blog/post/rendering-big-lists-in-react, I've had success using that technique on forms here github.com/richardscarrott/redux-form-v5-vs-redux-form-v6 (v5 implements that optimisation by splitting the inputs into fieldsets)riscarrott
Also, remember to test with a production build of React.riscarrott
@riscarrott Thanks man I'll try that. right now I'm little skeptical about my form which gets its data from portion of Redux store. so maybe I rewite that portion to Redux directly and it cause my form to update completely.Ehsan Shadi

1 Answers

1
votes

It can be extremely difficult to find issues like the above, because it totally depends on the actual code you have written. I am sure that the react rendering concept works (as it has for myself and so many others), but without seeing more code it is impossible to pinpoint the actual problem.

Still, I have a feeling that the real cause is "simple", meaning that it is very likely due to a prop being passed that has indeed changed. Maybe not in value, but in instance. IIRC, the PurerenderMixin is not comparing values (unless the data are value types), it is comparing instances. So even if the data is the same, if the instance has changed then it will re-render.

Some key-points to consider:

If you explicitly create a shouldComponentUpdate() method for the components then you can control this yourself. The PureRenderMixin does a shallow check to see if the props have changed, but, if you are passing them all then no matter if they have been used or not in the render method then just one of them changing will mean that the component is considered to need to render (as in, not mount, but render in memory and reconcile possible changes with the dom). Using shouldComponentUpdate can help stop rendering based on your own checks.

Using immutable props can also be helpful. The React PureRenderMixin only does a shallow comparison, as mentioned. So, if you are passing data-structures in the props that have depth (like sub-properties), then these are not checked. Meaning that you could end up with the opposite problem you have now, that is components that should have updated but aren't. If you are using i.e. redux and have done "the right thing" and are using i.e. Immutable.js, then using the PureRenderMixin is good enough.

Reading the specific docs for this may or may not help. There's some good description of this in the React manuals: https://facebook.github.io/react/docs/advanced-performance.html#shouldcomponentupdate-in-action

And - I forgot to say, if you are using redux then you could consider these somewhat elegant and simple ways of handling your forms:

Hope that this helps. Good luck.