Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

274
Views
Typescript type for React's useState() setter when

I'm trying to create React Context which parameters mode and setMode with are getter and setter of a React state. This is needed to be able to update the css mode (light / dark) from child components.

I'm getting this Typescript error for value of ColorModeContext (I guess, because this line of interface AppContextInterface is incorrect: setMode:(c: string) => void):

Error message

function App() {

    interface AppContextInterface {
        mode: string;
        setMode:(c: string) => void
    }

    const ColorModeContext = React.createContext<AppContextInterface>({
        mode: 'dark', // set a default value
        setMode: () => {},
    })

    const [mode, setMode] = React.useState<PaletteMode>('dark');

    // Update the theme only if the mode changes
    const theme = React.useMemo(() => createTheme(getTheme(mode)), [mode]);

    return (
        <ColorModeContext.Provider value= {{ mode, setMode }}>
            <ThemeProvider theme={theme}>
                <div>
                    </ChildComponent>
                </div>
            </ThemeProvider>
        </ColorModeContext.Provider>
    );
}
export default App;

How can I fix it? Thanks

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Since your useState type is defined as PaletteMode you need to define the same thing in your context's interface. In addition, you can also use the same types used to define the setState action (which you can import from React):

import { useState, createContext, Dispatch, SetStateAction} from 'react';

interface AppContextInterface {
  mode: PaletteMode;
  setMode: Dispatch<SetStateAction<PaletteMode>>
}

function App() {
  const ColorModeContext = createContext<AppContextInterface>({
      mode: 'dark', // set a default value
      setMode: () => {},
  })

  const [mode, setMode] = useState<PaletteMode>('dark');
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!