45715

Reactjs: Stop rerendering on a particular state change with shouldComponentUpdate()

Question:

I have multiple setState in my component onload. My page is re-rendering on click of dropdown values, because on-click I'm storing those values via setState.

Here to stop re-rendering on click, I'm using the below code.

shouldComponentUpdate() { return false }

Now because of the above function, My values are not storing in state on page load. Is there anything I need to do extra ?

Answer1:

You states don't show a change in your component, because it doesn't rereder at all, since you have returned false from the shouldComponentUpdate method.

If you want to restrict rendering on particular state change then just check for that state and return false else return true like

shouldComponentUpdate(nextProps, nextState) { if(this.state.dropdownVal != nextState.dropdownval) { return false } return true }

However its not such a bad thing to rerender because React rendering process is very efficient and you would want to show the updated state in the DOM more often then not.

Answer2:

You have to use the shouldComponentUpdate() like

shouldComponentUpdate(nextProps, nextState) { return !Immutable.is(this.state.synergy, nextState.synergy) }

Answer3:

The answer: you need to re-render. There's nothing wrong with this. However, if you have a lot of logic built in to your render statements (which you shouldn't do, logic should be happening in your containers) this can be cumbersome. Use element key's efficiently, and there's nothing wrong with re-rendering. You're looking for a way to subvert the design patterns of React, when you should instead be conforming in MOST cases.

Recommend

  • Getting a line that has the coordinates defined by the mouse location
  • Embedding a Google map
  • How to force Composer to download a local package?
  • Is it possible to send skype chat messages from a linux server without X?
  • How Get arguments value using inline assembly in C without Glibc?
  • jQuery: add elements until a particular height is reached
  • How to make R's read_csv2() recognise the text characters properly
  • pillow imaging ImportError
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • Implementation of State Monad
  • Reduction and collapse clauses in OMP have some confusing points
  • Loading .coffee files via a view in Rails
  • Create DicomImage from scratch using Dcmtk
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Projection media query: browser support and workarounds?
  • Modifying destination and filename of gulp-svg-sprite
  • Sending data from AppleScript to FileMaker records
  • How to handle AllServersUnavailable Exception
  • How to model a transition system with SPIN
  • VBA Convert delimiter text file to Excel
  • ORA-29908: missing primary invocation for ancillary operator
  • R: gsub and capture
  • Calling of Constructors in a Java
  • Traverse Array and Display in markup
  • Transpose CSV data with awk (pivot transformation)
  • How to disable jQuery.jplayer autoplay?
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Load html files in TinyMce
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • How to stop GridView from loading again when I press back button?
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?