React Native上的Redux无法从reducer

时间:2018-03-25 11:39:40

标签: javascript reactjs react-native redux redux-thunk

我是redux的新手,可能是一些愚蠢的错误。我正在尝试在Action中进行Api调用并将数据传递给Reducer。我可以通过action.data看到在reducer中正确传递的数据。我认为问题出在组件中的mapStateToProps中,因此我无法传递状态并呈现组件。请找到以下操作 - 减压器 - store.js - home.js

ACTION.JS

export const DATA_AVAILABLE = 'DATA_AVAILABLE';


export function getData(){
    return (dispatch) => {

        //Make API Call
   
        fetch("MY API URL").then((response) => {
          return response.json();
        }).then((data) => {
                    var data  = data.articles;
                    console.log(data)
                    dispatch({type: DATA_AVAILABLE, data:data});
        })
    };
}

这是Reducers.JS

import { combineReducers } from 'redux';

import { DATA_AVAILABLE } from "../actions/" //Import the actions types constant we defined in our actions

let dataState = {
  data: [],
  loading:true
};

const dataReducer = (state = dataState, action) => {
    switch (action.type) {
        case DATA_AVAILABLE:
        state = Object.assign({}, state, {
              data: [
                ...action.data //update current state data reference
              ],
              loading: false

            });
console.log(action.data);
            return state;
        default:
            return state;
    }
};

// Combine all the reducers
const rootReducer = combineReducers({
    dataReducer
    // ,[ANOTHER REDUCER], [ANOTHER REDUCER] ....
})

export default rootReducer;

这是带有Redux-thunk的Store.js

import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';

import reducers from '../app/reducers/index'; //Import the reducer

// Connect our store to the reducers
export default createStore(reducers, applyMiddleware(thunk));

最后是home.js组件,当我需要传递新状态并渲染它时

'use strict';

import React, { Component } from 'react';
import {
    StyleSheet,
    FlatList,
    View,
    Text,
    ActivityIndicator
} from 'react-native';

import {bindActionCreators} from 'redux';
import { connect } from 'react-redux';

import * as Actions from '../actions'; //Import your actions

class Home extends Component {
    constructor(props) {
        super(props);
        this.state = {
        };
    }

    componentDidMount() {
        this.props.getData(); //call our action
    }

    render() {
        if (this.props.loading) {
            return (
                <View style={styles.activityIndicatorContainer}>
                    <ActivityIndicator animating={true}/>
                </View>
            );
        } else {
          console.log(this.state)
            return (
              <View style={styles.row}>
                  <Text style={styles.title}>
                  {this.props.data}
                  fomrmo
                  </Text>
                  <Text style={styles.description}>
                  </Text>
              </View>
            );
        }
    }


};



// The function takes data from the app current state,
// and insert/links it into the props of our component.
// This function makes Redux know that this component needs to be passed a piece of the state
function mapStateToProps(state, props) {

    return {
        loading: state.dataReducer.loading,
        data: state.dataReducer.data
    }

}

// Doing this merges our actions into the component’s props,
// while wrapping them in dispatch() so that they immediately dispatch an Action.
// Just by doing this, we will have access to the actions defined in out actions file (action/home.js)
function mapDispatchToProps(dispatch) {
    return bindActionCreators(Actions, dispatch);
}

//Connect everything
export default connect(mapStateToProps, mapDispatchToProps)(Home);

1 个答案:

答案 0 :(得分:0)

假设:

    case DATA_AVAILABLE:
      console.log(action.data.length) 

将使console.log超过0

更改减速器动作:

const dataReducer = (state = dataState, action) => {
    switch (action.type) {
        case DATA_AVAILABLE:
          return {
            ...state,
            data: action.data,
            loading: false    
          });
        default:
            return state;
    }
};

解决:

  

对象无效作为React子对象(找到具有Keys的对象)   {source,author,title,description,url})

那是因为你试图渲染Object:

{this.props.data}

但如果你这样做:

{
   this.props.data.map((el, i) => 
     <p key={i}>Element nr {i}</p> 
   )
}

它应该有用。