Skip to content

Instantly share code, notes, and snippets.

@ihollander
Last active April 12, 2021 13:13
Show Gist options
  • Save ihollander/c37853751cced1cce7ff8fe17a37a4bb to your computer and use it in GitHub Desktop.
Save ihollander/c37853751cced1cce7ff8fe17a37a4bb to your computer and use it in GitHub Desktop.
React Process

Building React features

  1. Decide if you need state. Do we need state for this feature? If so, what components need access to that state? Initialize state in the closest common parent component to all the componenets that need access to it.
  2. Set up the initial state. What's a good initial value? What will we see on the page first? How will it change?
  3. Set up component to render something based on state. Do we need conditional rendering?
  4. Find a way to update state dynamically (based on user interaction). Add an event handler to a DOM element, and update state from the event handler.

In depth explanation: https://reactjs.org/docs/thinking-in-react.html

Using Inverse Data Flow

To update state in a parent component from an event in a child component:

  1. Define a callback function in the parent component that sets state
  2. Pass the callback function as a prop to the child
  3. Create an event handler in the child component
  4. Call the callback in the event handler with whatever data we're sending up to the parent

Example: https://codesandbox.io/s/120920-inverse-data-flow-exercise-tbhk9?file=/src/App.js

Creating a Controlled Form

  1. For each input field in the form, define a state variable using useState
  2. Connect the input field's value to the state variable <input value={state} />
  3. Set up an onChange handler and update state when the input field's value changes: <input onChange={e => setState(e.target.value) />
  4. Set up an onSubmit hanlder on the <form> and use the data from state when the form is submitted (i.e. make a POST request)

Example: https://codesandbox.io/s/120920-form-exercise-forked-07qop?file=/src/App.js

Fetching data when a component first renders

  1. Create an initial state with a value of an empty array, or null, to represent the initial value
  2. Set up a useEffect hook with an empty array as a second argument useEffect(() => {}, [])
  3. In the callback to useEffect, write your fetch function to GET data from the server. When you get the response, set state. fetch(url).then(r => r.json).then(setState)

Example: https://codesandbox.io/s/120920-inverse-data-flow-exercise-tbhk9?file=/src/App.js

Note: You don't need to use useEffect for all fetch requests. Typically they are only used with GET requests. You can write other fetch requests in event handlers (onClick, onSubmit, etc).

Filtering/Sorting Features

When you have a feature that involves being able to sort or filter a list, you will need to add state and use that state to deterimine which items are visible.

  1. Create a state variable to represent how you are filtering: const [phase, setPhase] = useState(1)
  2. Use that variable to create a filtered array of the visible items you'd like to display: const visibleItems = items.filter(item => item.phase === phase)
  3. Use the filtered list to display the items: const itemComponents = visibleItems.map(item => <Item key={item.id} item={item} />
  4. Update the state variable in order to change which items are displayed: <button onClick={() => setPhase(2)>

Example: https://github.com/learn-co-students/nyc04-seng-ft-120720/blob/main/35-state-events/project-showcase/src/components/ProjectList.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment