0

I'm trying to pass "section" and "setSection" from parent to child.

I was following this but it didn't work for me: Passing useState as props in typescript

Error: Unhandled Runtime Error TypeError: setSection is not a function

parent component:

export default function CareerProgression() {
   const [section, setSection] = useState< 'video' | 'condensed' | 'full' >('condensed');

   return (
      <ModuleToggle_withState section={section} setSection={setSection} />
   );
}

child:

import { Dispatch, useEffect, SetStateAction } from 'react';

export function ModuleToggle_withState(section:any, setSection:Dispatch<SetStateAction<any>>) {
// Currently I'm using "any" just so I can test passing useState props

   return (
      <>
         <SegmentedControl
            defaultValue='video'
            value={section}
            onChange={(value: 'video' | 'condensed' | 'full') => setSection(value)}
         />
      </> 
   ); 
}
Lewis
  • 135
  • 1
  • 10

1 Answers1

4

Components get only one argument which is an object

ModuleToggle_withState({
  section,
  setSection
}: {
  section:any,
  setSection:Dispatch<SetStateAction<any>>
})
Konrad
  • 21,590
  • 4
  • 28
  • 64
  • Thanks but I'm still getting "TypeError: setSection is not a function" on this line: onChange={(value: 'video' | 'condensed' | 'full') => setSection(value)} – Lewis Jan 18 '23 at 22:06