Git Product home page Git Product logo

eslint-plugin-valtio's Introduction

eslint-plugin-valtio

Valtio linting plugin for better development.

Installation

npm install eslint-plugin-valtio --save-dev

for yarn users:

yarn add -D eslint-plugin-valtio 

Usage

Add valtio to the extends section of your .eslintrc configuration file.

{
  "extends": [
      "plugin:valtio/recommended"
    ]
}

Alternatively, you can enable rules in the plugin, selectively.

{
  "rules": {
    "valtio/state-snapshot-rule": "warn",
    "valtio/avoid-this-in-proxy" :"warn",
  } 
}

Why

This plugin helps you catch common mistakes that can occur in valtio. Here are some cases that this plugin catches.

Snapshots in callbacks are not recommended

We shouldn't use snapshots in callbacks, because snapshots can be stale there.

const state = proxy({ count: 0 })

function App() {
  const snap = useSnapshot(state)
  const handleClick = () => {
    console.log(snap.count) // This is not recommended as it can be stale.
  }
  return (
    <div>
      {snap.count} <button onClick={handleClick}>click</button> 
    </div>
  )
}

Proxies in render phase is not recommended

In render phase, it's better to use snapshots (as they're made to be compatible with react's reactivity) instead of states directly.

const state = proxy({ count: 0 })

function App() {
  return (
    <div>
      {state.count} // This is not recommended as it is not reactive.
    </div>
  )
}

Snapshots mutating is not possible

Snapshots are made to be used in the react render phase, and not mutable. So, we need to mutate proxy states directly.

const state = proxy({ count: 0 })

function App() {
  const snap = useSnapshot(state)
  const handleClick = () => {
    ++snap.count // This doesn't work. Use proxy state instead.
  }
  return <button onClick={handleClick}>mutate</button> 
}

Computed declaration order

In the way valtio treats objects in proxyWithComputed, the order of fields matters; for example, quadrupled comes before doubled, but it depends on doubled, so the order is wrong! So we need to bring doubled first.

const state = proxyWithComputed({
  count: 0,
}, {
  quadrupled: (snap) => snap.doubled * 2, // Not found, If a computed field deriving value is created from another computed, the computed source should be declared first.
  doubled: (snap) => snap.count * 2,
})

Using this in proxy (valtio/avoid-this-in-proxy)

When working with proxy using this in it's context as long as taken care of will work as expected but since the implementation differs from a simple proxy to a snapshot version of the proxy, using this could get confusing. This rule is specifically for beginners that are adapting to the structure/differences in valtio.

const state = proxy({
  count: 0,
  inc() {
    ++state.count; // works as the state is being modified
  },
});

const state = proxy({
  count: 0,
  inc() {
    ++this.count; // works as the context belongs to proxy
  },
});

const state = snapshot(
  proxy({
    count: 0,
    inc() {
      ++this.count; // won't work since the params are now forzen since you are in a snapshot.
    },
  })
);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.