首页 > 其他分享 >[React] Valtio proxy-state management lib intro

[React] Valtio proxy-state management lib intro

时间:2022-12-21 16:26:32浏览次数:66  
标签:count Valtio management lib component React state proxy

https://bestofjs.org/projects/valtio

Cool things about Valtio, it is completely independ from React component.

It is self testable and hook with React very well.

import {proxy, useSnapshot} from 'valtio'
import {saveInfo} from './api';

const state = proxy({
  count: 0,
  setCount: (count: number) => {
    state.count = count
  },
  inc: () => {
    state.count++
  },
  onClick: async () => {
    // an async operation here
    // such as api call and setTimeout
    // will not trigger react reredner the component
    // if using it inside react component, need to be careful
    await saveInfo();
    state.inc()
  }
})

export default function App() {
  const snap = useSnapshot(state)
  
  return (
    <>
      <h1>Count: {snap.count}</h1>
      <button onClick={snap.onClick}>+1</button>
    </>
  )
}

 

标签:count,Valtio,management,lib,component,React,state,proxy
From: https://www.cnblogs.com/Answer1215/p/16996468.html

相关文章