nihgwu / react-native-dva-starter

a React Native starter powered by dva and react-navigation
641 stars 149 forks source link

处理根据user状态处理登录跳转 #21

Closed coderguai closed 7 years ago

coderguai commented 7 years ago

如何根据user的login状态跳转登录页或者主页

kala888 commented 7 years ago

判断login状态,直接dispatch就是啦

coderguai commented 7 years ago

嗯,但是我不知道在那里写这个逻辑,是在路由model的subscriptions里处理吗 @kala888

nihgwu commented 7 years ago

我的做法是在 appModal 里写一个 loadStorage 的 effect,然后在 subscriptions 里面调用,最后 SplashScreen.hide()

coderguai commented 7 years ago

嗯,我在appModal的effect里用select获取app的login状态,但为什么login状态一直是false(我之前已经登录过了),第二次应该为true呀,是我select的地方不对吗,请问该在那里去获取app的login状态?

nihgwu commented 7 years ago

你可以看看你的状态树啊,实际的login状态是什么就知道了

coderguai commented 7 years ago

我看过了,login为true啊,但是subscriptions的调用是在persist状态恢复之前调用的,所以login就是false了,请问这种情况该怎么处理?

nihgwu commented 7 years ago

我不用 redux-persist,自己管理 storage

coderguai commented 7 years ago

嗯,dva如何自己管理storage哦,能给个demo参考一下吗 @nihgwu

nihgwu commented 7 years ago

自己用 AsyncStorage 存数据,跟 dva 无关

coderguai commented 7 years ago

嗯,我用AsyncStorage是在model的effects处理存取操作了,但是还是取不到值,能给点思路吗,谢谢 @nihgwu

nihgwu commented 7 years ago
    *loadStore(action, { call, put }) {
      const didGuide = yield call(Store.get, 'didGuide')
      if (!didGuide) {
        yield put({ type: 'showGuide' })
      }
      const setting = (yield call(Store.get, 'setting')) || defaultSetting
      yield put({
        type: 'updateStore',
        payload: {
          setting,
        },
      })
      yield delay(500)
      SplashScreen.hide()
    },
    *showGuide(action, { put }) {
      yield delay(2000)
      yield put(NavigationActions.navigate({ routeName: 'Guide' }))
      Store.set('didGuide', true)
    },
coderguai commented 7 years ago

你好,我现在有个问题就是Store封装的get方法怎么处理callback哦,在effects中还是取不到值,但在comment是可以的? @nihgwu

nihgwu commented 7 years ago

https://github.com/nihgwu/react-native-sudoku/blob/master/app/utils/store.js

coderguai commented 7 years ago

嗯,非常感谢🙏 @nihgwu