簡體   English   中英

React-native 不使用 Redux 操作重新渲染

[英]React-native not rerendering with Redux action

我有一個組件,當用戶通過身份驗證時應該導航:

componentDidUpdate(prevProps, prevState) {
    if (this.props.authenticated) {
      this.props.navigation.navigate('Main')
    }
  }

當我調度 authLogin 時,它應該導致重新渲染,它處理導航:

export const authLogin = (username, password) => {
  return dispatch => {
    dispatch(authStart());
    axios.post(`http://10.0.2.2:8000/api/v1/rest-auth/login/`, {
      username: username,
      password: password
    })
      .then(response => {
        var token = response.data.key;
        try {
          AsyncStorage.setItem('token', token);
        } catch (err) {
          console.log(err)
        }
        dispatch(authSuccess(token));
      })
      .catch(err => {
        dispatch(authFail());
        console.log(err);
      })
  }
}

這是我的減速器:

export default function (state = initialState, action) {
  switch (action.type) {
    case "AUTH_START": {
      return {
        ...state,
        authenticating: true,
      }
    }
    case "AUTH_SUCCESS": {
      return {
        ...state,
        authenticating: false,
        authenticated: true,
        token: action.token,
      }
    }
    case "AUTH_FAIL": {
      return {
        ...state,
        authenticating: false,
        authenticated: false,
      }
    }
    case "AUTH_LOGOUT": {
      return {
        ...state,
        authenticating: false,
        authenticated: false,
        token: null,
      }
    }
    default:
      return state
  }
}

和動作創造者:

export const authStart = () => ({type: "AUTH_START"})
export const authSuccess = token => ({type: "AUTH_SUCCESS", token})
export const authFail = () => ({type: "AUTH_FAIL"})

我的控制台記錄 Redux 操作已分派並且狀態正在更改,但沒有發生重新渲染。 這是整個組件:

import React, { Component } from 'react';
import { View, StyleSheet } from 'react-native';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import LoginForm from '../components/LoginForm';
import { authLogin } from '../actions/authActions';

export class LoginScreen extends Component {
  handlePress = async (username, password) => {
    await this.props.authLogin(username, password);
  }

  componentDidUpdate(prevProps, prevState) {
    if (this.props.authenticated) {
      this.props.navigation.navigate('Main')
    }
  }

  render() {
    return (
      <View style={styles.loginForm}>
        <LoginForm handlePress={this.handlePress} {...this.props} />
      </View>
    );
  }
}

const mapState = state => {
  return {
    authenticated: state.auth.authenticated
  }
};

const mapDispatch = dispatch => {
  return bindActionCreators({
    authLogin,
  }, dispatch)
};

export default connect(mapState, mapDispatch)(LoginScreen);

LoginScreen.propTypes = {
  authLogin: PropTypes.func.isRequired,
  authenticated: PropTypes.bool.isRequired,
};

const styles = StyleSheet.create({
  loginForm: {
    justifyContent: 'center',
    alignItems: 'center',
    flex: 1
  }
});

這是我的商店:

import {  combineReducers } from 'redux';
import { createStore, applyMiddleware } from 'redux';
import { logger } from 'redux-logger';
import thunk from 'redux-thunk';
import auth from './auth'

const reducer = combineReducers({auth})
const enhancer = applyMiddleware(thunk, logger)
const store = createStore(reducer, enhancer)

export default store

Store 在 App.js 中的 Provider 中連接。

我添加了另一個減速器,它立即修復了它。 顯然 redux 不喜歡 combineReducers() 只有一個參數。

即改變

const reducer = combineReducers({auth})

const reducer = combineReducers({auth, otherReducer})

為什么不將身份驗證檢查和導航語句放在 handlePress() 中。

handlePress = async (username, password) => {
    await this.props.authLogin(username, password);
    if (this.props.authenticated) {
      this.props.navigation.navigate('Main')
    }
  }

在 authLogin() 調度操作並更新狀態后,您可以檢查身份驗證狀態並導航用戶。

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM