reading-notes


Project maintained by mr-atta Hosted on GitHub Pages — Theme by mattgraham

Context API



Review, Research, and Discussion

Describe use cases useState() ๐Ÿ†š useReducer()โ“ ๐Ÿ“

useReducer is usually preferable to useState when you have complex state logic that involves multiple sub-values or when the next state depends on the previous one. useState vs useReducer

Why do custom hooks need the use prefixโ“ ๐Ÿ“

the โ€œuseโ€ prefix also helps in easily identifying if a function is a custom hook.

What do custom hooks usually doโ“ ๐Ÿ“

Custom Hooks are a mechanism to reuse stateful logic , but every time you use a custom Hook, all state and effects inside of it are fully isolated. How does a custom Hook get isolated state? Each call to a Hook gets isolated state.

Using any list of custom hooks, research and name one that you think will be useful in your applicationsโ“

useState

Describe how a hook that fetches API data might workโ“ ๐Ÿ“

Put the fetchData function above in the useEffect hook and call it




Context

Context provides a way to pass data through the component tree without having to pass props down manually at every level.

When to Use Context

note : If you only want to avoid passing some props through many levels, component composition is often a simpler solution than context.

API


hooks and context example

Snackbars in React: An Exercise in Hooks and Context.

Snackbars

const { addAlert } = useSnackBars()
> ...
> addAlert('Your profile is updated!')




๐Ÿ“Œ context api

๐Ÿ“Œ hooks and context example

๐Ÿ“Œ react context links