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

No warning for snapshots in callbacks are not recommended #19

Closed
devnomic opened this issue Apr 12, 2022 · 6 comments · Fixed by #20
Closed

No warning for snapshots in callbacks are not recommended #19

devnomic opened this issue Apr 12, 2022 · 6 comments · Fixed by #20

Comments

@devnomic
Copy link

I literally coped the example. No eslint warning shows. I'm pretty sure my valtio eslint config is working, since if i write state.count in render, it will show a warning from (valtio/state-snapshot-rule).

const state = proxy({ count: 0 })

function App() {
  const snap = useSnapshot(state)
  const handleClick = () => {
    console.log(snap.count) // <- No warning shows here
  }
  return (
    <div>
      {snap.count} <button onClick={handleClick}>click</button> 
    </div>
  )
}
@dai-shi
Copy link
Member

dai-shi commented Apr 12, 2022

Thanks for reporting. Hmmm, not sure what's happening.

@barelyhuman Are you still around? Can take time to investigate it?

@barelyhuman
Copy link
Collaborator

always here 😂, yeah i’ll check it out

@barelyhuman
Copy link
Collaborator

It's a conflict between #16 and the Readme,

in the past fix we consider it okay to be read but not assigned to, so it let's it past the warning, removing the isReadOnly check would basically mean to revert fixes from #16

@dai-shi
Copy link
Member

dai-shi commented Apr 13, 2022

Thanks for the investigation!

Can we check the useEffect deps, and if so skip it?

btw, how does this work? (does lint pass with/without #16?)

function useExample0(s: typeof state) {
  const snap = useSnapshot(s.a1);

  const c = snap.b.c;
  useEffect(() => {
    if (c === 'a1c') {
      state.a1.b.c = 'example';
    }
  }, [c]);
}

this is less error-prone.

@barelyhuman
Copy link
Collaborator

barelyhuman commented Apr 13, 2022

If #16 is disabled

It still breaks, but that's been fixed and the lint does warn about the snapshot being used as expected.

Can we check the useEffect deps, and if so skip it?

So isReadOnly to add a check for the caller being useEffect or useCallback and that the dep uses the called snapshot (or snapshot propery)? It'll still break the test cases in #16 which it should 😂 , but yeah, we'll have to add a comment in #16 to let the devs there know that it's supposed to be in the deps to be considered valid.

@barelyhuman barelyhuman mentioned this issue Apr 13, 2022
@dai-shi
Copy link
Member

dai-shi commented Apr 15, 2022

Published: https://www.npmjs.com/package/eslint-plugin-valtio/v/0.4.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants