美文网首页
React Hookx

React Hookx

作者: 1182a58bbdaa | 来源:发表于2019-01-09 23:21 被阅读0次

React Hookx

Installing

React Hookx requires React 16.7.0-alpha.0 and React Dom 16.7.0-alpha.0 or later.

Using npm:

npm install --save react-hookx

Using yarn:

yarn add react-hookx

Example

// store/index.js
import Hookx from 'react-hookx'

const state = {
  name: '',
  age: 0,
}

const action = {
  SetUser({ setState }, data) {
    setState({
      name: data.name,
      age: data.age,
    })
  },
  SetUserSync({ setState }, data) {
    return new Promise((resolve, reject) => {
      try {
        setTimeout(() => {
          const nextState = setState({
            name: data.name,
            age: data.age,
          })
          resolve(nextState)
        }, 1000)
      } catch (err) {
        reject(err)
      }
    })
  },
}

const storage = {
  mode: 'session',
  reducer: state => ({
    name: state.name,
    age: state.age,
  }),
}

export default new Hookx({
  state,
  action,
  storage,
})

// index.js
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
import store from './store'
import './index.css'

const { StoreProvider } = store
ReactDOM.render(
  <StoreProvider>
    <App />
  </StoreProvider>,
  document.getElementById('root')
)
import React from 'react'
import store from '../store'

function TestComponent(props) {
  const { useDispatch, useStoreState } = store
  const dispatch = useDispatch()
  const { name, age } = useStoreState()

  return (
    <>
      <div onClick={() => dispatch('SetUser', { name: 'zhang', age: 21 })}>
        name: {name}
        age: {age}
      </div>
      <button
        onClick={() =>
          dispatch('SetUserSync', { name: 'huang', age: 20 }).then(state => {
            console.log(state)
          })
        }
      >
        sync
      </button>
    </>
  )
}

export default TestComponent

API

state

Initialization data.

action

The only way to modify the state.

storage

mode

choose webStorage:

  • session
  • local
reducer

choose which state wanna to storage.

StoreProvider

useStoreState

customize hook
get react hookx state in component

const { useStoreState } = hookx
const { name, age } = useStoreState()

useDispatch

customize hook
get react hookx dispatch in component

const { useDispatch } = hookx
const dispatch = useDispatch()

License

ISC

相关文章

  • React Hookx

    React Hookx Installing React Hookx requires React 16.7.0-...

  • React基础

    react 教程 react 组件介绍 react state 介绍 react Props 介绍 React:组...

  • 学习react no.1

    学习react no.1 react 定义 react 特点 react

  • React Native 学习之路

    React 相关资料 React Components React Properties React State ...

  • React基础

    React包含react元素和react组件 react元素 react组件 react组件分为函数组件和类组件 ...

  • React面试题 整理脑图

    react基础 React生命周期 react-router react进阶 react Hooks redux 其他

  • react 导入中的 as

    import React from 'react'只导入 是 React。 而import * as React ...

  • ES5与ES6小结部分

    1var React=require('react'); 等价 import React from ' react...

  • React DnD基础概念和整体架构

    React DnD 的英文是 Drag and Drop for React。React DnD 是 React ...

  • React

    React 《React 官网文档》 React简介 React概念 React官网学习实践 - jSX简介 Re...

网友评论

      本文标题:React Hookx

      本文链接:https://www.haomeiwen.com/subject/qcmlvxtx.html