“无法在现有日期转换期间更新”在 react-native 中使用带有 redux 的 react-navigation

任务

我刚刚开始学习 react-native 和 redux。我正在尝试实现身份验证方案。我正在使用react-navigation.

import { authenticate } from './../actions';

const Component = (props) => {
  const { dispatch, user } = props;

  // user comes from state (using mapStateToProps)

  let phone = '';
  let password = '';

  if (user.errors != null) {
    alert(user.errors);
  }

  if (user.authenticated) {
      dispatch(NavigationActions.navigate({ routeName: 'LoggedIn' }));
  }

  async function onLogin() {    
    dispatch(authenticate(phone, password));
  }

  return (
    <View>
      <TextInput
        style={{height: 40, width: '100%', borderColor: 'gray', borderWidth: 1, marginTop: 10}}
        placeholder="(xxx) xxx-xx-xx"
        keyboardType={'phone-pad'}
        onChangeText={(value) => phone = value}
        value={phone}
      />
      <TextInput
        style={{height: 40, width: '100%', borderColor: 'gray', borderWidth: 1, marginTop: 10}}
        placeholder="Пароль"
        onChangeText={(value) => password = value}
        value={password}
      />
      <Button
        onPress={onLogin}
        title="Continue"
      />
    </View>
  );
}

这是身份验证操作

export function authenticate(phone, password) {
  return async dispatch => {
    dispatch(authenticating());
    let response;
    try {
      response = await Api.authenticate(phone, password);
    }
    catch (e) {
      dispatch(authenticateFailure(e.error));
      return;
    }
    dispatch(authenticateSuccess(response.token, response.payload));
  };
};

所以基本上当authenticate动作被调度并且用户成功通过身份验证时,我想将用户重定向到下一个屏幕(LoggedIn)。但重定向后我收到此错误:

Warning: Cannot update during an existing state transition (such as within render or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to componentWillMount.

我猜问题是前一个组件在重定向到下一个组件(LoggedIn)时被中断渲染,因为每个调度都会触发重新渲染。

分派操作后如何导航到另一个屏幕?我可以以某种方式在不触发重新渲染的情况下分派动作吗?

甘尼什尾巴

尝试将所有这些移动到

componentWillReceiveProps(nextProps) {
   // check props you want to use as execution trigger 
 }

#

const { dispatch, user } = props;

// user comes from state (using mapStateToProps)

let phone = '';
let password = '';

if (user.errors != null) {
  alert(user.errors);
}

if (user.authenticated) {
    dispatch(NavigationActions.navigate({ routeName: 'LoggedIn' }));
}

async function onLogin() {    
  dispatch(authenticate(phone, password));
}

你可能需要调整一些你在渲染方法中使用的常量

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

React Native / Redux-setState-在现有状态转换期间无法更新

来自分类Dev

Redux React-Native(在react-redux中使用useSelector与使用connect有什么区别?)

来自分类Dev

如何在带有钩子的React Native中使用refs?

来自分类Dev

使用带有redux的react-native-router-flux,如何更新视图组件中的状态?

来自分类Dev

无法使用react native在redux的reducer中的购物车中添加现有产品

来自分类Dev

无法使用react native在redux的reducer中的购物车中添加现有产品

来自分类Dev

React Navigation 没有集成到 Redux 中?

来自分类Dev

React Native - Redux ~ 没有被调用时更新道具

来自分类Dev

在React / React Native(Redux)中使用Spread运算符

来自分类Dev

Connected Component的prop在带有Redux的React Native中不会更新

来自分类Dev

组件的 prop 不会在带有 Redux 的 React Native 中更新

来自分类Dev

使用 React-Navigation 在所有屏幕中显示组件 - React Native

来自分类Dev

在带有SSR的React Redux架构中使用history.js存储browserHistory

来自分类Dev

React Native 中的 redux-form 有问题

来自分类Dev

没有redux的react native app的文件夹结构

来自分类Dev

react-native: undefined 不是带有反应导航的对象(评估“_this2.props.navigation”)

来自分类Dev

在React Navigation上更新react-native-maps

来自分类Dev

安装React Navigation后无法构建或安装React Native应用

来自分类Dev

react native:使用 headerRight 导航时,react-navigation 崩溃

来自分类Dev

使用嵌套的 react-navigation 来 React Native HOC

来自分类Dev

在 React Native 中使用 Redux 连接()每个容器视图?

来自分类Dev

无法使用带有uri的React Native显示图像

来自分类Dev

在使用react-redux实现react-native时出错

来自分类Dev

如何使用带有触发器的 react-navigation 从 FlatList react-native 获取详细数据是来自本机基础的按钮 onPress()?

来自分类Dev

带有Django的CSRF,使用Axios的React + Redux

来自分类Dev

带有 redux 的 react-preloader-icon

来自分类Dev

React Native / Redux-无法访问Redux存储

来自分类Dev

React Native,无法通过使用react -redux在购物车中添加项目

来自分类Dev

React - setState errir - 在状态转换期间无法更新

Related 相关文章

  1. 1

    React Native / Redux-setState-在现有状态转换期间无法更新

  2. 2

    Redux React-Native(在react-redux中使用useSelector与使用connect有什么区别?)

  3. 3

    如何在带有钩子的React Native中使用refs?

  4. 4

    使用带有redux的react-native-router-flux,如何更新视图组件中的状态?

  5. 5

    无法使用react native在redux的reducer中的购物车中添加现有产品

  6. 6

    无法使用react native在redux的reducer中的购物车中添加现有产品

  7. 7

    React Navigation 没有集成到 Redux 中?

  8. 8

    React Native - Redux ~ 没有被调用时更新道具

  9. 9

    在React / React Native(Redux)中使用Spread运算符

  10. 10

    Connected Component的prop在带有Redux的React Native中不会更新

  11. 11

    组件的 prop 不会在带有 Redux 的 React Native 中更新

  12. 12

    使用 React-Navigation 在所有屏幕中显示组件 - React Native

  13. 13

    在带有SSR的React Redux架构中使用history.js存储browserHistory

  14. 14

    React Native 中的 redux-form 有问题

  15. 15

    没有redux的react native app的文件夹结构

  16. 16

    react-native: undefined 不是带有反应导航的对象(评估“_this2.props.navigation”)

  17. 17

    在React Navigation上更新react-native-maps

  18. 18

    安装React Navigation后无法构建或安装React Native应用

  19. 19

    react native:使用 headerRight 导航时,react-navigation 崩溃

  20. 20

    使用嵌套的 react-navigation 来 React Native HOC

  21. 21

    在 React Native 中使用 Redux 连接()每个容器视图?

  22. 22

    无法使用带有uri的React Native显示图像

  23. 23

    在使用react-redux实现react-native时出错

  24. 24

    如何使用带有触发器的 react-navigation 从 FlatList react-native 获取详细数据是来自本机基础的按钮 onPress()?

  25. 25

    带有Django的CSRF,使用Axios的React + Redux

  26. 26

    带有 redux 的 react-preloader-icon

  27. 27

    React Native / Redux-无法访问Redux存储

  28. 28

    React Native,无法通过使用react -redux在购物车中添加项目

  29. 29

    React - setState errir - 在状态转换期间无法更新

热门标签

归档