Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why there is error when set function in function's arguments?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

// 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>
  );
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading