skip to Main Content

I want to set function in function’s arguments like this:

export const Mode = (mode, setMode) => {

  mode == "dark" ? setMode("light") : setMode("dark")

}

and result was setMode is not a funtion

2

Answers


  1. First you add this hook :

    const [mode, setMode] = useState("dark");
    

    And export function Mode

    export const Mode = (mode, setMode) => {
      setMode(mode === "dark" ? "light" : "dark");
    };
    
    Login or Signup to reply.
  2. // Define the Mode function
    export const toggleMode = (currentMode, setModeFunction) => {
      if (currentMode === "dark") {
        setModeFunction("light");
      } else {
        setModeFunction("dark");
      }
    }
    
    // Usage within a React component
    import React, { useState } from 'react';
    import { toggleMode } from './pathToYourToggleModeFunction';
    
    const MyComponent = () => {
      const [mode, setMode] = useState("light");
    
      return (
        <div>
          <p>Current mode is: {mode}</p>
          <button onClick={() => toggleMode(mode, setMode)}>Toggle Mode</button>
        </div>
      );
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search