Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hookstate error 111 doesn't exist #332

Closed
mattangus opened this issue Aug 29, 2022 · 23 comments
Closed

Hookstate error 111 doesn't exist #332

mattangus opened this issue Aug 29, 2022 · 23 comments
Labels
documentation An issue requires changes in the documentation only

Comments

@mattangus
Copy link

My application has thrown a hookstate error 111 with the link https://hookstate.js.org/docs/exceptions/#hookstate-111. This does not exist in the documentation.

@avkonst
Copy link
Owner

avkonst commented Sep 4, 2022

yes, it is v4 exception. It means you are switching a state source on rerender, ie. the initial value to useHookstate points to a different State on rerender.
In code the exceptions is: InitStateStoreSwitchover
The docs will get updated for this exception.

@ayodeji-jackson
Copy link

Is there a way to turn this off or bypass it somehow? It's causing hot module replacement in vite.js to fail.

@avkonst
Copy link
Owner

avkonst commented Sep 4, 2022

hmm... "It's causing hot module replacement in vite.js to fail." this should not be the cause... but when it throws this exception, what property do you acccess? what type is it? what object type is it?

@ayodeji-jackson
Copy link

ayodeji-jackson commented Sep 5, 2022

I don't even need to access anything. When vite tries to perform hmr after detecting changes in my code, the app crashes and this error is shown in the console. But then if i make another code change, hmr works.

@avkonst
Copy link
Owner

avkonst commented Sep 5, 2022

could you please create a reproducer repo? what state value do you provide to hookstate?

@ayodeji-jackson
Copy link

https://github.com/ayodeji-jackson/vendre

I have two global states using hookstate. both are arrays.

@avkonst
Copy link
Owner

avkonst commented Sep 5, 2022

thanks. I will have a look later in the evening. It looks like Vite reloads globals but does not reload React state behind useState, which is clearly a problem, which makes useState bug-prone under hot reload in Vite. You basically end up with one state behind a global variable and another one captured in component state on the previous rerender. This makes the globals leaking under hmr in vite. I do not think this is the issue with react-scripts, but I will double check. I am not sure what to do to fix it: removing the exception is possible but the problems will appear later in the app, leaving exception is a problem for vite...

@ayodeji-jackson
Copy link

oh yeah. keeping previous state on component re-render is one of vite's features. do you think using hookstate for all local states will fix it? or i could check if disabling this feature is an option.

@avkonst
Copy link
Owner

avkonst commented Sep 9, 2022

local state is not impacted.. it is a global one i think... please let me know if it can be tuned...

@HidayetCanOzcan
Copy link

When setting your states, use arrow function to prevent this error, like;

const test = hookstate(null);

export function useTestState() {
return useHookstate(()=>test); //Do not useHookstate(test);
}

hope this helps.

@oskarengstrom
Copy link

oskarengstrom commented Nov 27, 2022

Having the same issues in Next.js v13.0.5. Getting Error: HOOKSTATE-111 [path: /] when hot module reloading.

useHookstate(()=>test) does not work for me sadly. The error is gone, but the code doesn't work as before.

Any other ways to get around this? 🙏🏻

@HidayetCanOzcan
Copy link

If error is gone after using arrow function, check your code again, because if that was the reason you are getting 111 error it means your state was updating one step behind. You maybe adapted this, structured your code and now your code doesnt work it used to be :) I am talking blindly of course, maybe some code snippets?

@oskarengstrom
Copy link

Yeah, sorry, should have posted a snippet. Here goes:

// pages/index.js 

import { useHookstate, none, hookstate } from "@hookstate/core";

const globalState = hookstate([
  { task: "do one thing" },
  { task: "do enother thin gs" },
]);

export default function Home() {
  const todos = useHookstate(globalState);

  return (
    <div style={{ display: "grid", placeItems: "start" }}>
      {todos.map((todo, i) => (
        <Todo todo={todo} key={i} />
      ))}
      <button onClick={() => todos.merge([{ task: "new todo" }])}>
        Add new
      </button>
    </div>
  );
}

const Todo = ({ todo }) => {
  return (
    <div>
      <input
        value={todo.task.get()}
        onChange={(e) => todo.task.set(e.target.value)}
      />
      <button onClick={() => todo.set(none)}>Delete</button>
    </div>
  );
};

In this example above I get the 111 error on hmr, but the code works apart from that.

If I do const todos = useHookstate(() => globalState); the error disappears, but todo.task.get() suddenly isn't getting the value but returns a Proxy instead.

@HidayetCanOzcan
Copy link

HidayetCanOzcan commented Dec 1, 2022

Try to seperate your states and import them and you wont get this error and your hooks will work as you wanted. I think the cause of the problem is strict mode.

Or a better solution will be using useHookstateMemo

function useGlobalState(){
const globalState = useHookstateMemo(()=>[
{ task: "do one thing" },
{ task: "do enother thin gs" },
{ task: "do enother thin gs" },
],[]);
return useHookstate(globalState)
}

and you can use it in your component like;

const todos = useGlobalState();

@HidayetCanOzcan
Copy link

@avkonst you should close some of the issues, I really like hookstate but its getting harder to convince people to use this awesome tool. :)

@mattangus
Copy link
Author

this is now part of the docs: https://hookstate.js.org/docs/exceptions/#hookstate-111
closing.

@avkonst
Copy link
Owner

avkonst commented Dec 3, 2022

@HidayetCanOzcan sure... I am just overloaded with stuff. 60+ hours per week at work. And it is Summer time where I live. But I will clean it up soon.

@avkonst
Copy link
Owner

avkonst commented Dec 3, 2022

The error 111 is not part of the docs yet, but I will update soon

@avkonst avkonst reopened this Dec 3, 2022
@mattangus
Copy link
Author

Oops saw 101 and read 111 😅

@oskarengstroem
Copy link

Just wanna confirm that this worked for me! Thanks a lot!

function useGlobalState(){ const globalState = useHookstateMemo(()=>[ { task: "do one thing" }, { task: "do enother thin gs" }, { task: "do enother thin gs" }, ],[]); return useHookstate(globalState) }

and you can use it in your component like;

const todos = useGlobalState();

@HidayetCanOzcan
Copy link

Just wanna confirm that this worked for me! Thanks a lot!

function useGlobalState(){ const globalState = useHookstateMemo(()=>[ { task: "do one thing" }, { task: "do enother thin gs" }, { task: "do enother thin gs" }, ],[]); return useHookstate(globalState) }
and you can use it in your component like;
const todos = useGlobalState();

No problem, glad to help :)

@avkonst avkonst added the documentation An issue requires changes in the documentation only label Dec 18, 2022
avkonst added a commit that referenced this issue Dec 18, 2022
@avkonst
Copy link
Owner

avkonst commented Dec 18, 2022

see related follow up #364

@avkonst
Copy link
Owner

avkonst commented Dec 18, 2022

docs updated

@avkonst avkonst closed this as completed Dec 18, 2022
shinyjohn0401 pushed a commit to shinyjohn0401/hookstate that referenced this issue Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation An issue requires changes in the documentation only
Projects
None yet
Development

No branches or pull requests

6 participants