react-simply
react-simply copied to clipboard
Invalid Hook call
I'm receiving this error regarding line 35 of your script.
Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
- You might have mismatching versions of React and the renderer (such as React DOM)
- You might be breaking the Rules of Hooks
- You might have more than one copy of React in the same app See https://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem.
Try renaming to useStore
import React, { createContext, useContext, useReducer } from "react";
export const StateContext = createContext();
export const StateProvider = ({ reducer, initialState, children }) => (
<StateContext.Provider value={useReducer(reducer, initialState)}>
{children}
</StateContext.Provider>
);
export const useStore = () => useContext(StateContext);
Hi, could you please post the code where you are actually using the state?
As the custom hooks rules said, your custom hooks function name must start with use
to make rules of hooks apply to it.
change this:
export const getState = () => useContext(StateContext)
to this:
export const useState = () => useContext(StateContext)
Neither the original code, nor the suggested fix passes the linter for hook rules
I've opened #6 that addresses this issue using @BensonLiao 's suggestion, it seems to work