Home > Enterprise >  Typescript Type of React useState second parameter
Typescript Type of React useState second parameter

Time:03-16

i am trying to apply typescript on my react javascript project

i used createContext() to pass useState() parameters

and applying typescript, he rages because of lack of first value in createContext()

also i am not sure what type i should put for 'setIsOpen'

I wish your help, if you have any answers...

i leave my screenshot of my code below enter image description here

export const SmartAccordion = ({children}:SmartAccordionProps) => {
  const [isOpen, setIsOpen] = useState(false);

  const value = {
    isOpen,
    setIsOpen
  };

  return (
    <AccordionContext.Provider value={value}>
      <S.Wrapper>{children}</S.Wrapper>
    </AccordionContext.Provider>
  );
};

type AccordionContextType = {
  isOpen: boolean;
  setIsOpen: any;
}


const AccordionContext = createContext();
const useAccordionContext = () => useContext(AccordionContext);

export const SmartAccordionHeader = ({children, className}:SmartAccordionHeaderProps) => {
  const { isOpen, setIsOpen } = useAccordionContext();

  return (
    <S.HeaderWrapper
      onClick={() => setIsOpen(!isOpen)}
      className={className}
    >
      {children}
    </S.HeaderWrapper>
  );
};

CodePudding user response:

React.Dispatch<React.SetStateAction<boolean>>

  • Related