Socket
Book a DemoInstallSign in
Socket

@mng12345/m-state

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mng12345/m-state

use hooks and closure to make a state manager ### 1. change react state ### 2. change the state anywhere ### 3. get current state value

latest
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

use hooks and closure to make a state manager

1. change react state

2. change the state anywhere

3. get current state value

example

const husband = atom({
  defaultValue: {
    name: 'zm',
    age: 27
  },
  key: 'husband'
})

const wife = atom({
  defaultValue: {
    name: 'zl',
    age: 28
  },
  key: 'wife'
})

const AllAge = () => {
  const [husbandState, setHusband] = useMState(husband)
  const [wifeState, setWife] = useMState(wife)
  return (
    <>
      allAge: {husbandState.age + wifeState.age}<hr/>
      <button onClick={() => {
        setHusband({
          ...husbandState,
          age: husbandState.age + 1
        })
        setWife({
          ...wifeState,
          age: wifeState.age + 1
        })
      }}>
      addAllAge
      </button>
  </>
)
}

const App = (): JSX.Element => {

  const [husbandState, setHusband] = useMState(husband)
  const [wifeState, setWife] = useMState(wife)
  const addHusbandAge = () => {
    setHusband({
      ...husbandState,
      age: husbandState.age + 1
    })
  }
  const addWifeAge = () => {
    setWife({
      ...wifeState,
      age: wifeState.age + 1
    })
  }

  return (
    <div>
      husband: {husbandState.name}<hr/>
      wife: {wifeState.name}<hr/>
      husband age: {husbandState.age}<hr/>
      wife age: {wifeState.age}<hr/>
      <button onClick={addHusbandAge} style={{
        marginRight: 10
      }}>addHusbandAge</button>
      <button onClick={addWifeAge}>addWifeAge</button>
      <hr/>
      <AllAge />
    </div>
    )
}

render(<App />, document.getElementById('app'))

FAQs

Package last updated on 02 Mar 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts