react-model
react-model copied to clipboard
[request] Can we use react-model within taro?
via: https://nervjs.github.io/taro/docs/hooks.html
It requires a more generic implementation of useStore.
src/index.tsx
import { PureComponent, useEffect, useState } from 'react'
// 👇 👇
// import { useEffect, useState } from '@tarojs/taro'
// .......
const useStore = (modelName: string, depActions?: string[]) => {
const setState = useState(Global.State[modelName])[1]
useEffect(() => {
Global.uid += 1
const hash = '' + Global.uid
if (!Global.Setter.functionSetter[modelName]) {
Global.Setter.functionSetter[modelName] = {}
}
Global.Setter.functionSetter[modelName][hash] = { setState, depActions }
return function cleanup() {
delete Global.Setter.functionSetter[modelName][hash]
}
}, [])
const updaters = getActions(modelName, { setState, type: 'function' })
return [getState(modelName), updaters]
}
I have implemented a lib for Taro, see https://github.com/kyleslight/react-model-taro