2021年3月28日星期日

How can i update context value with the state value

I can receive the correct object, but i can't update the context value with the updated object with the state value

In the:

const Context = createContext({    theme: dark,    toggleTheme: () => {},  });  

I want to make the theme: dark, become, theme: defaultTheme, with a dynamic value modified by useState

import React, { createContext, useState } from 'react';    import light from '../themes/light';  import dark from '../themes/dark';    const Context = createContext({    theme: dark,    toggleTheme: () => {},  });    export default Context;    export function ThemeContextProvider({ children }) {    const [defaultTheme, setDefaultTheme] = useState(dark);      function toggleTheme() {      setDefaultTheme(defaultTheme === dark ? light : dark);    }        return (      <Context.Provider value=>      {children}      </Context.Provider>    )  }  
https://stackoverflow.com/questions/66847916/how-can-i-update-context-value-with-the-state-value March 29, 2021 at 10:03AM

没有评论:

发表评论