r/reactjs • • Jul 01 '18

Help Beginner's Thread / Easy Question (July 2018)

Hello! just helping out /u/acemarke to post a beginner's thread for July! we had almost 550 Q's and A's in last month's thread! That's 100% month on month growth! we should raise venture capital! /s

Got questions about React or anything else in its ecosystem? Stuck making progress on your app? Ask away! We’re a friendly bunch. No question is too simple. You are guaranteed a response here!

New to React? Free, quality resources here

Want Help on Code?

  • Improve your chances of getting helped by putting a minimal example on to either JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new). Describe what you want it to do, and things you've tried. Don't just post big blocks of code.
  • If you got helped, pay it forward! Answer questions even if there is already an answer - multiple perspectives can be very helpful to beginners. Also there's no quicker way to learn than being wrong on the Internet.
51 Upvotes

454 comments sorted by

View all comments

Show parent comment

2

u/acemarke Jul 03 '18

React's default behavior is that when a component re-renders, all of its immediate children re-render, and so on cascading down the subtree. So yes, if your parent component re-renders, the child <Map> and <Marker> components will as well. There's no "I rendered children A and B using this.state.firstThing, and children B and C using this.state.secondThing, and only firstThing updated, so only A and B should update". It's all or nothing.

Where shouldComponentUpdate comes into play is that a child component can check to see if its props are the same as last time, in which case it can say "oh hey, I already know my output won't change, there's no point in me re-rendering", and return false. So, if your child <Marker> components implemented shouldComponentUpdate, they could potentially skip re-rendering.

Having said that, a couple observations:

  • There's nothing wrong with child components re-rendering by default - that's how React works!
  • If you do try to implement shouldComponentUpdate in <Marker>, the most common approach is a "shallow equality" check, which just compares all incoming props by reference to the last set of props. But, in your case, you're passing a new position object in every time, so even if the lat and lon fields are the same, the object containing them is different. You'd either need to make sure the parent passes the same position object each time it re-renders, or have the child implement shouldComponentUpdate using a more careful check.

Honestly, though, I wouldn't worry about trying to optimize perf at this point unless the app is running really slowly. Focus on getting the logic "right", then optimize.

1

u/swyx Jul 03 '18

im guessing he's using some 3rd party thing for his Map, so the re render is really obvious, thats why he's bothering about it right now. had this exact thing before when using Mapbox.

1

u/abigreenlizard Jul 03 '18

Hey, thanks for the reply. Ok so it's an absolute rule that if a child of A updates, then A updates as well?

So, if your child <Marker> components implemented shouldComponentUpdate, they could potentially skip re-rendering.

When you say this, do you mean only in the case that the parent is not updating already? If the parent is updating, then the child must as well. But if the parent isn't updating, and the child has it's state/props changed, AND we don't want the child to change as a result, only then is shouldComponentUpdate really useful. Is that correct?

The thing that is annoying me is that when I click on a marker to open it's infowindow, all the markers get redrawn (the marker on click changes the parent state) It's only a split-second, and functionally doesn't matter, but it's bothering me enough that I've ended up on quite the deep dive into state/props trying to see what I can do about it (we have to present a prototype next Wednesday, you understand).

I've been trying to implement shouldComponentUpdate in the Marker component, I think that should work. I've just created a CustomMarker class that implements shouldComponentUpdate and renders a Marker, passing in it's props. It's not working at all atm (markers are in the DOM and I can log their properties ok, just not showing up on the map for some reason), but I'm sure I can get it.

Thanks for the advice

2

u/swyx Jul 03 '18

btw it sounds like ur using mapbox, its been almost a year since i touched that sort of stuff but if u would like a sample app check out https://github.com/sw-yx/FSA-React-Trip-Planner/tree/master/react-tripplanner-live-ENDHERE