Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I replace context with hooks?

Is there a way with new react hooks API to replace a context data fetch?

If you need to load user profile and use it almost everywhere, first you create context and export it:

export const ProfileContext = React.createContext()

Then you import in top component, load data and use provider, like this:

import { ProfileContext } from 'src/shared/ProfileContext'

<ProfileContext.Provider
      value={{ profile: profile, reloadProfile: reloadProfile }}
    >
        <Site />
    </ProfileContext.Provider>

Then in some other components you import profile data like this:

import { ProfileContext } from 'src/shared/ProfileContext'
const context = useContext(profile);

But there is a way to export some function with hooks that will have state and share profile with any component that want to get data?

like image 648
ZiiMakc Avatar asked Nov 02 '18 14:11

ZiiMakc


People also ask

Do Hooks and context replace Redux?

But now it's possible to replace Redux with React Hooks and the Context API. In this tutorial, you're going to learn a new way of handling state in your React projects, without writing excessive code or installing a bunch of libraries — as is the case with Redux.

When should you not use Hooks?

Don't call Hooks inside loops, conditions, or nested functions. Instead, always use Hooks at the top level of your React function, before any early returns. By following this rule, you ensure that Hooks are called in the same order each time a component renders.

Which hook is the replacement for context API?

Using React ContextAPI + useReducer as a replacement of Redux as a State Management Architecture. With addition of a new React API called “Hooks” (since version 16.8), we've got a new way of manipulate the state of our application and other features in React, without the necessity of components in shape of a class.

Can you use context in Hooks?

React ContextIt can be used together with the useState Hook to share state between deeply nested components more easily than with useState alone.


2 Answers

React provides a useContext hook to make use of Context, which has a signature like

const context = useContext(Context);

useContext accepts a context object (the value returned from React.createContext) and returns the current context value, as given by the nearest context provider for the given context.

When the provider updates, this Hook will trigger a rerender with the latest context value.

You can make use of it in your component like

import { ProfileContext } from 'src/shared/ProfileContext'

const Site = () => {
   const context = useContext(ProfileContext);
   // make use of context values here
}

However if you want to make use of the same context in every component and don't want to import the ProfileContext everywhere you could simply write a custom hook like

import { ProfileContext } from 'src/shared/ProfileContext'
const useProfileContext = () => {
   const context = useContext(ProfileContext);
   return context;
}

and use it in the components like

const Site = () => {
   const context = useProfileContext();
}

However as far a creating a hook which shares data among different component is concerned, Hooks have an instance of the data for them self and don'tshare it unless you make use of Context;

like image 59
Shubham Khatri Avatar answered Sep 20 '22 22:09

Shubham Khatri


updated:

My previous answer was - You can use custom-hooks with useState for that purpose, but it was wrong because of this fact:

Do two components using the same Hook share state? No. Custom Hooks are a mechanism to reuse stateful logic (such as setting up a subscription and remembering the current value), but every time you use a custom Hook, all state and effects inside of it are fully isolated.

The right answer how to do it with useContext() provided @ShubhamKhatri

like image 44
Melounek Avatar answered Sep 17 '22 22:09

Melounek