r/reactjs • u/swyx • 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
- Read the new, official Getting Started page on the docs
- /u/acemarke's suggested resources for learning React and his React/Redux links list.
- Kent Dodds' Egghead.io course
- Tyler McGinnis' 2018 Guide
- Codecademy's React courses
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.
53
Upvotes
View all comments
2
u/SelkiesTheEndless Jul 07 '18
Hey all,
I have what I think are some basic react/redux questions. I have been working with the frameworks for a while but I think I still have some basic misunderstandings. I am building a chat application which seems like the perfect use case for them but I will try to start with generic questions before getting more specific for my application.
Question: Maybe it is premature optimization but that seems like the sort of thing that could add a lot of overhead, I would think to add a "winner" field to the state but maybe its really not that complex an operation and is fine.
2) An extension of the first. In Redux there is a library called Reselect which I understand is for memoizing calculations made from state. BUT I am not sure how to use it to get the most benefits, in the example above it Reselecting/Memoizing the Winner call would be rather useless because the only time where things really happen the input (squares) would have changed. Example in my App:
State { AllMessages: {}, Colors: {} }If I want to have a Selector for
AllMessagesFromJimmmywhere I filter AllMessages byMessage.From === JimmyI can see the use case for that but as soon as another message is added to the state I believe it would need to be recalculated because the input, AllMessages, has changed. If something happens in theColorspart of the state we would already haveAllMessagesFromJimmybut 99% of the churn to the state is in Messages. This would only improve performance in edge cases but I guess its better than nothing? Again this seems like it might not be the most performant way to do things.Question: Even though
AllMessagesFromJimmycan be calculated from the state is this the type of data that should be stored rather than calculated?3) When using react-redux I am struggling with when to use Connected components and trying to figure out if there is a good reason not to use them everywhere. I have attached an image of a generic message. https://imgur.com/a/YQTXmOg At first I thought it made sense to just have the Message be a Connected component, messages should be pretty static and when they change just re-render the whole thing. BUT it turns out in my application there are lots of additional features that might make this less ideal. You can have users with a nickname (Jimmy becomes James), display the company name or not, change to a 24 hour clock, highlight keywords, etc. So now if I toggle one of those features I don't want to have to re-render the whole message and it might make sense to just Connect each one directly to the State.
Question: Should every piece of the UI be
Connectedto the state? Will this be less performant because even thoughRenderis called lessMapStateToPropsis now called constantly. When should I be usingShouldComponentUpdatevs Connecting everything?Thanks to anyone and everyone in advance! I am really appreciative and enjoying learning these things.