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

1

u/lvsanche Jul 20 '18

how can I do this when I need to render elements that the user will interact with? For example a sign up page. I could make a component that has a couple of label elements paired with input elements and a submit button. If I split between view and fetch would this mean that the inner element is getting passed a prop for the labels to be displayed, as well as a props for the input values ?

Additionally, I would need to pass a callback for the state of the outer component to save the input and set in motion the sign up action when the submit button is pressed. Does that mean that the outer component would take care of error detection as well as then calling the API to sign in ? And what about components that contain other components that are themselves outer/inner components? That's were I see a lot of my code break this pattern. I have a component that has a couple of divs with different components that are themselves two levels.

1

u/Awnry_Abe Jul 21 '18
  1. Yes, in general you've got the basic idea. We don't pass labels to inner components though. Business logic and such is handled away from the view, so events are pass up <Inner onSubmit={this.handSubmit}>. Our login screen is one place that gets no pre-filled form data (from our app). Where there are things like a list of items and a detail view, we don't let the list component say or do anything beyond saying "row X was clicked". This lets the list view be used in a variety of pages.
  2. Yes, those inner, view-only components can be composed of other, complex components. You'll have visual containers that have css to visually carve up the page that dole out the real work to more complex components that fetch data, then pass that data down to their view. And that layering does a rinse and repeat until only views exist at the end of the chain.