0

I am currently asking myself the following question: Is it recommended that I define my state and logic directly in the ContextProvider or is it okay if I define the state and logic in a separate function to separate the code a bit?

Example:

const MyContext = React.createContext({});

const createStore = () => {
    const [myState, setMyState] = useState();
    return {
       myState,
       setMyState
    }
}

const MyContextProvider = ({ children }) => {
  const store = createStore();
  return (
     <MyContext.Provider value={store}>{children}</MyContext.Provider>
  )
}

I am a little bit affraid of that createStore function. Does the createStore always gets recreated if the Provider rerenders ?

Edit: Thanks for the answer!

What if I want to use a parameter in the useCreateStore hook ? Will the parameter gets updated?

Example:

const MyContext = React.createContext({});

const useCustomStore= (myAwesomeValue) => {
    const [myState, setMyState] = useState();

   const doSomething = useCallback(() => {
      //
   }, [myAwesomeValue])

    return {
       myState,
       setMyState
    }
}

const MyContextProvider = ({ children, title }) => {
  const { myState } = useCustomStore(title); //You need to desctructure the returned object here, note myState
  return (
     <MyContext.Provider value={myState}>{children}</MyContext.Provider>
  )
}

1 Answer 1

1

What you are trying to create for your "store" is called a custom hook

You will need to make some changes though. It is customary to use 'use' as the start of a custom hook. so, here I have renamed createStore to useCustomStore. Since it is a custom hook with useState, it follows the same rules as if you actually had it within your context provider

Also, your custom hook returns an object which contains the state and a mutation method. you will need to access the state either directly store.myState or you can destructure it { myState} as I have in the example.

const MyContext = React.createContext({});

const useCustomStore= () => {
    const [myState, setMyState] = useState();
    return {
       myState,
       setMyState
    }
}

const MyContextProvider = ({ children }) => {
  const { myState } = useCustomStore(); //You need to desctructure the returned object here, note myState
  return (
     <MyContext.Provider value={myState}>{children}</MyContext.Provider>
  )
}

Is the same as

const MyContext = React.createContext({});
  
const MyContextProvider = ({ children }) => {

  const [myState, setMyState] = useState();

  return (
     <MyContext.Provider value={myState}>{children}</MyContext.Provider>
  )
}

So rerenders will preserve state, since it uses the useState hook.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks you for the clear answer :) I already thought that I have to prefix it with use, because React is doing some magic under the hood with that prefix.
You don't need to use the use prefix, you can all the custom hook whatever you like.. its just "good practice" to do so. Yes, you should pass an initial state into the hook useState(initialState), and then mutate the state using the setMyState mutation method provided by the useState hook
I have edited my main post and added a different question. I was not able to post code in this comment section here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.