2

How can I get the setDay function to work in typescript, this was working fine in javascript, the context should only receive numbers as arguments :

const SetScreen = React.createContext();

  const { setDay }  = useContext(SetScreen);


  return (
    <nav>
      {programDays.map((days, idx) => {
        return (
          <button
            key={days}
            onClick={() => {
              setDay(idx);
                          }}
                     >
                    </button>
        );
      })}
      </nav>
  );
};

export default Nav;
function App() {
  const [day, setDay] = useState(0);
  return (
    <div className="App">
      <SetScreen.Provider value={day}>
        <Nav />
        <DailySchedule day={day} />
      </SetScreen.Provider>
    </div>
  );
João Pedro
  • 794
  • 2
  • 12
  • 28
  • 1
    You need to create an interface or a type which matches the type of the value of the context, and pass it to the `createContext` call i.e. `React.createContext()`. – cbr Jan 12 '21 at 10:08
  • the value will be a number, so I've tried const SetScreen = React.createContext(0) , but then I get "Property 'setDay' does not exist on type 'Number'" as an error – João Pedro Jan 12 '21 at 10:12
  • That's not the value of the context. The value of the context is what `useContext()` returns. If you need help with creating the type, please edit your post to include the component where you create the state, i.e. the component which uses `` – cbr Jan 12 '21 at 10:14

0 Answers0