1

I'm trying to learn TypeScript with React and I'm struggling to use React Context with TypeScript. There's a Context below that I'm trying to turn into TypeScript, I managed to create the context for the menu and it's working fine. Here is it.

import { createContext, useContext, ReactNode, SetStateAction, Dispatch, useState } from "react";



// define the type for createContext
interface MenuStateContextType {
    activeMenu: boolean
    setActiveMenu: Dispatch<SetStateAction<boolean>>;
}

// create and initialize createContext with a default value
export const MenuStateContext = createContext<MenuStateContextType>({
    activeMenu: true,
    setActiveMenu: () => { }
});




// define react's "children" prop type
type ContextProviderProps = {
    children?: ReactNode
}

export const ContextProvider = ({ children }: ContextProviderProps) => {

    const [activeMenu, setActiveMenu] = useState(true);


    return (
        <MenuStateContext.Provider
            value={{
                activeMenu,
                setActiveMenu,
            }}
        >
            {children}
        </MenuStateContext.Provider>
    )
}


// passing the MenustateContext to useContext() in order to use in the app
export const useMenuStateContext = () => useContext(MenuStateContext)

but I'm struggling to implement the initialState code. I would really appreciate it if someone could help me turning this JSX code into TSX.

import React, { createContext, useContext, useState } from 'react';

const StateContext = createContext();

const initialState = {
  chat: false,
  cart: false,
  userProfile: false,
  notification: false,
};

export const ContextProvider = ({ children }) => {
  const [screenSize, setScreenSize] = useState(undefined);
  const [currentColor, setCurrentColor] = useState('#03C9D7');
  const [currentMode, setCurrentMode] = useState('Light');
  const [themeSettings, setThemeSettings] = useState(false);
  const [activeMenu, setActiveMenu] = useState(true);
  const [isClicked, setIsClicked] = useState(initialState);

  const setMode = (e) => {
    setCurrentMode(e.target.value);
    localStorage.setItem('themeMode', e.target.value);
  };

  const setColor = (color) => {
    setCurrentColor(color);
    localStorage.setItem('colorMode', color);
  };

  const handleClick = (clicked) => setIsClicked({ ...initialState, [clicked]: true });

  return (
    // eslint-disable-next-line react/jsx-no-constructed-context-values
    <StateContext.Provider value={{ currentColor, currentMode, activeMenu, screenSize, setScreenSize, handleClick, isClicked, initialState, setIsClicked, setActiveMenu, setCurrentColor, setCurrentMode, setMode, setColor, themeSettings, setThemeSettings }}>
      {children}
    </StateContext.Provider>
  );
};

export const useStateContext = () => useContext(StateContext);

1 Answer 1

0

You must provide initialState to createContext function and must provide types.

    //Instead of this code.....
    const StateContext = createContext();
    
    const initialState = {
      chat: false,
      cart: false,
      userProfile: false,
      notification: false,
    };


    //Use the following code.....
     interface stateContextType={
      chat:boolean,
      cart:boolean,
      userProfile:boolean,
      notification:boolean
     }

     const initialState = {
      chat: false,
      cart: false,
      userProfile: false,
      notification: false,
     };

    const StateContext = createContext<stateContextType>(initialState);
Sign up to request clarification or add additional context in comments.

Comments

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.