Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Object is not a function?

I just started learning React Hooks. I have a file called appContext.js ..Which has the AppContext inside

const AppContext = React.createContext(initialState);

And I want to use it in file checkInfo.js

const CheckInfo = (props) => {

    const [state, dispatch] = useContext(AppContext);

    useEffect(() => {
          var personData = {};

          async function fetchData() {

            dispatch({
              type: "isLoding",
              payload: true,
            });
          }
          ////other code
        }

but i have

TypeError: Object is not a function

Where am I wrong?

like image 769
zahra zamani Avatar asked Dec 31 '22 20:12

zahra zamani


1 Answers

replace the line

const [state, dispatch] = useContext(AppContext);

to

const { state, dispatch }  = useContext(AppContext);

since useContext returns an object with fields state and dispatch - not an array

like image 115
Tibebes. M Avatar answered Jan 02 '23 11:01

Tibebes. M