Hooks and Context are not for managing application state in lieu of Redux.
Context is more akin to props in that it solves the nightmare that can become of communicating information from a parent to a child in large, heavily nested enterprise level applications. The downside is that Context is a bit more complex and setting it up can be painful.
Hooks just allow us to use functional components that can now hook into application state without having to turn them into class-based components.
The confusion lies in that you can grab functionality from Redux with a hook like useReducer
without the traditional Redux setup.
So like if you were working on a blog application and you wanted to add functionality to update your blogposts, you can apply useReducer
like so:
const blogReducer = (state, action) => {
switch (action.type) {
case 'add_blogpost':
return [...state, { title: `Blog Post #${state.length + 1}` }];
default:
return state;
}
};
So that would be your reducer which you would then apply like so:
export const BlogProvider = ({ children }) => {
const [blogPosts, dispatch] = useReducer(blogReducer, []);
You can temporarily create a helper function to dispatch an action object:
const addBlogPost = () => {
dispatch({ type: 'add_blogpost' });
};
You would have to add it to your value prop, the 'add_blogpost'
. Anyway, it's just a confusing way of utilizing aspects of Redux on a functional component without using the whole Redux system itself, but again, not a replacement.