React Native fetch 不适用于 redux?

React Native fetch won't work with redux?

我正在尝试对我自己用 NodeJS 编写的后端进行简单的提取调用。虽然,我很难让提取工作。我在 React Native 中将 Redux Thunk 与 Redux 一起使用来整理状态和道具,以便制作一个自我导航的登录/注册视图。我的操作看起来像这样:

actions.js

import {
    LOGIN,
    LOGIN_SUCCESS,
    LOGIN_FAILED
} from '../constants/constants'

export function loginAPI() {
    return (dispatch) => {
      dispatch(userLogin())
      fetch('http://localhost:3000/users/')
      .then(data => data.json())
      .then(json => {
        console.log('json:', json)
        dispatch(userLoginSuccess(json.success))
      })
      .catch(err => dispatch(userLoginFailed(err)))
    }
  }

function userLogin() {
    return {
        type: LOGIN,
    }
}

function userLoginSuccess(token) {
    return {
        type: LOGIN_SUCCESS,
        token,
    }
}

function userLoginFailed() {
    return {
        type: LOGIN_FAILED,
    }
}

user.js (减速器)

import {
    LOGIN,
    LOGIN_SUCCESS,
    LOGIN_FAILED,
    } from '../constants/constants'

const initialState = {
    token: "",
    isFetching: false,
    error: false
}

export default function reducers(state = initialState, action){
    switch (action.type) {
        case LOGIN:
          return {
            ...state,
            isFetching: true,
            token: ""
          }
        case LOGIN_SUCCESS:
          return {
            ...state,
            isFetching: false,
            token: action.token
          }
        case LOGIN_FAILED:
          return {
            ...state,
            isFetching: false,
            error: true
          }
        default:
          return state
      }
}

最后,这是我的渲染页面。

Login.js

import React from 'react'
import { TouchableHighlight, View, Text, StyleSheet } from 'react-native'

import { connect } from 'react-redux'
import { loginAPI } from '../actions/actions'

let styles

const Login = (props) => {
  const {
    container,
    text,
    button,
    buttonText
  } = styles

  const { token, isFetching } = props.user;
  console.log('Token: ', props.token);

  return (
    <View style={container}>
      <Text style={text}>Redux Example</Text>
      <TouchableHighlight style={button} onPress={() => props.loginAPI()}>
        <Text style={buttonText}>Login</Text>
      </TouchableHighlight>
      {
        isFetching && <Text>Loading</Text>
      }
      {
        token ? (<Text>Name: {token}</Text>) : null
      }
    </View>
  )
}

styles = StyleSheet.create({
  container: {
    marginTop: 100,
    paddingLeft: 20,
    paddingRight: 20
  },
  text: {
    textAlign: 'center'
  },
  button: {
    height: 60,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#0b7eff'
  },
  buttonText: {
    color: 'white'
  }
})

function mapStateToProps (state) {
  return {
    user: state.user
  }
}

function mapDispatchToProps (dispatch) {
  return {
    loginAPI: () => dispatch(loginAPI())
  }
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(Login)

这里的问题是提取调用似乎永远不会到达后端。我在后端有一个日志,告诉何时对 API 进行调用,但它从未出现。调试,检查状态显示 success (token) 为 null,这是可以理解的,因为从未进行过调用。到用户的路由 returns 一个 JSON 对象,其中包含一个值 "success",这是真的。为什么我的登录名API 没有获取?

最后这是我的 configStore

configStore.js

import { createStore, applyMiddleware } from 'redux'
import app from '../reducers'
import thunk from 'redux-thunk'

export default function configureStore() {
  let store = createStore(app, applyMiddleware(thunk))
  return store
}

从 2995 到 3005 的端口转发 UDP/TCP 都解决了这个问题,并且使用了我的 IPv4 地址而不是本地主机。