thoughtSpace
TwitterGithubRSS Feed

Note Space

Hints, cheat sheets and notes on code.

Home

React Crash Course

Posted on March 8, 2021
react-crash-course

A declarative, efficient, and flexible JavaScript library for building user interfaces.

React. js is an open-source JavaScript library that is used for building user interfaces specifically for single-page applications. It's used for handling the view layer for web and mobile apps. React also allows us to create reusable UI components.

React is a free and open-source front-end JavaScript library for building user interfaces based on UI components. It is maintained by Meta and a community of individual developers and companies.

In a controlled component, form data is handled by a React component. The alternative is uncontrolled components, where form data is handled by the DOM itself.

To write an uncontrolled component, instead of writing an event handler for every state update, you can use a ref to get form values from the DOM.

Development tips

// Use react query to fetch data and not useEffect
const { status, data, error, isFetching } = useQuery(
  ['data'],
  async () => {
    const data = await (
      await fetch(`${API_BASE_URL}/data`)
    ).json()
    return data
  }
)

React.Strictmode - StrictMode is a tool for highlighting potential problems in an application. Like Fragment, StrictMode does not render any visible UI. It activates additional checks and warnings for its descendants. StrictMode currently helps with:

  • Identifying components with unsafe lifecycles
  • Warning about legacy string ref API usage
  • Warning about deprecated findDOMNode usage
  • Detecting unexpected side effects
  • Detecting legacy context API
  • Ensuring reusable state

Short-circuit evaluation.

{condition && <ConditionalComponent />}

I.e., if the first operand (condition) is falsy, the AND operator (&&) stops and it does not evaluate the second operand ().

  • Reason why eslint issues a warning when you use a function inside an array, the function is re-allocated on render because it's within render scope, and therefore the function reference changes between renders, causing your useEffect to every time (based on shallow comparison of the dep array), so useCallback avoids that and keeps the reference the same between renders.
// Linting problem when a function is called in an array, a hack
const fetchNextUser = useRef(() => {});
fetchNextUser.current = () => {logic};
useEffect(() => { fetchNextUser.current() }, []);
  • useContext hook, work with context api, share data without passing props ```javascript const moods = { happy: 'happy', sad: 'sad'} const MoodContext = createContext(moods);

const App = (props) => { return ( <MoodContext.Provider value={moods.happy}> </MoodContext.Provider>) }

const MoodEmoji = () => { const mood = useContext(MoodContext); // Consume value from nearest parent provider

return

{ mood }

}


- useReducer, hook that is used to handle reactive data using a redux pattern
```javascript
const App = () => {
 function reducer(state, action) {
     switch (action.type) {
       case 'increment':
         return state + 1;
       case 'decrement':
         return state - 1;
       default:
         throw new Error();
     }
 }

const [state, dispatch] = useReducer(reducer, 0);

return (
 <>
 Count : {state}
 <button onClick={() => dispatch({type: 'decrement'})}>-</button>
 <button onClick={() => dispatch({type: 'increment'})}>+</button>
 </>)}