react-simply icon indicating copy to clipboard operation
react-simply copied to clipboard

Invalid Hook call

Open Darksoulsong opened this issue 5 years ago • 5 comments

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:

  1. You might have mismatching versions of React and the renderer (such as React DOM)
  2. You might be breaking the Rules of Hooks
  3. 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.

image

Darksoulsong avatar Apr 11 '19 13:04 Darksoulsong

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);

stevenambs avatar May 06 '19 06:05 stevenambs

Hi, could you please post the code where you are actually using the state?

lukashala avatar May 15 '19 08:05 lukashala

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)

BensonLiao avatar Jun 13 '19 08:06 BensonLiao

Neither the original code, nor the suggested fix passes the linter for hook rules

mgenev avatar Jul 22 '19 20:07 mgenev

I've opened #6 that addresses this issue using @BensonLiao 's suggestion, it seems to work

mattiaerre avatar Jul 25 '19 03:07 mattiaerre