byte-fe / react-model

The next generation state management library for React
235 stars 22 forks source link

[request] Can we use react-model within taro? #107

Open leecade opened 5 years ago

leecade commented 5 years ago

via: https://nervjs.github.io/taro/docs/hooks.html

ArrayZoneYour commented 5 years ago

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]
}
kyleslight commented 4 years ago

I have implemented a lib for Taro, see https://github.com/kyleslight/react-model-taro