【问题标题】:React - Redux - this.props.fetch.map is not a functionReact - Redux - this.props.fetch.map 不是函数
【发布时间】:2016-12-16 11:53:29
【问题描述】:

当我尝试通过 ma​​pStateToProps 将数据传递到我的组件时,我收到错误 this.props.fetch.map is not a function。如果我控制台徽标我的 Action 我得到一个 Object 。我不知道该怎么做将这个对象作为数组传递。有人可以帮助我吗?

这是我的组件 trabalhos.js

import React, { Component } from 'react';
import { connect } from 'react-redux';
import * as actions from '../actions';


class Trabalhos extends Component {

    componentWillMount(){
        this.props.fetchData();
    }


    renderList(){

        return _.map(this.props.fetch.trabalhos ,() => {

            return(

                <li key={this.props.fetch.trabalhos.miristica.id}>
                    <img src={this.props.fetch.trabalhos.miristica.img} /> 
                    <p>{this.props.fetch.trabalhos.miristica.descricao}</p>
                    <p>{this.props.fetch.trabalhos.miristica.nome}</p>            
                </li>

            );
        });
    }

    render(){
    return (

        <div>
            <div className="trabalhos">
                <div className="trabalhos_caixa">
                    <div className="row">
                        <div className="col-xs-12">
                            <ul className="no_pad">
                                {this.renderList()}
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    );
}
}


function mapStateToProps(state){

return { fetch: state.fetch };

}


export default connect(mapStateToProps, actions)(Trabalhos);

这是我的操作 index.js

import Firebase from 'firebase';
import { FETCH_DATA } from './types';

var firebase = require("firebase/app");
require("firebase/database");

var config = {
apiKey: "AIzaSyDi3f9pSGH833Hq3TBzibCK1SbPOheiGmE",
authDomain: "portofoliofirebase.firebaseapp.com",
databaseURL: "https://portofoliofirebase.firebaseio.com",
storageBucket: "portofoliofirebase.appspot.com",
messagingSenderId: "656734450041"
};

firebase.initializeApp(config);

var data = firebase.database().ref();

export function fetchData(){
return dispatch => {
    data.on('value', snapshot => {
        dispatch({
            type: FETCH_DATA,
            payload: snapshot.val()
        });
    });
};
}

这是我的减速器 fetch_reducer.js

import { FETCH_DATA } from '../actions/types';

export default function(state = [], action) {
console.log(action);
switch(action.type){
    case FETCH_DATA:
        return action.payload;
}

return state;
}

这是我的 rootReducer index.jx

import { combineReducers } from 'redux';
import fetchReducer from './fetch_reducer';

const rootReducer = combineReducers({

fetch: fetchReducer

});

export default rootReducer;

存储 index.js

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import { Router, browserHistory } from 'react-router';
import reducers from './reducers';
import routes from './routes';
import * as firebase from 'firebase';
import reduxThunk from 'redux-thunk'

const createStoreWithMiddleware = applyMiddleware(reduxThunk)(createStore);
const store = createStoreWithMiddleware(reducers);

ReactDOM.render(
<Provider store={store}>
<Router history={browserHistory} routes={routes} />
</Provider>
, document.querySelector('.container'));

谢谢!

【问题讨论】:

  • 你在哪里分配state.fetch'
  • 你好,Hitmands,对不起,我不明白你的意思!! state fetch 来自我的 rootReducer。
  • 发布您创建 state.fetch 属性的代码。
  • @Hseleiro 你确定 fetch in store 是一个数组而不是对象或其他任何东西。 Map 不是 Object 的原生函数,而只是 Array
  • @Hitmands,你是说我的 fetch_reducer 吗?它在我的问题中

标签: reactjs firebase redux react-redux redux-thunk


【解决方案1】:

我猜 Redux 存储的初始状态在你的情况下是空的。我的意思是 render 在任何 reducer 触发之前触发,所以 this.props.fetch === {}

请在createStore指定preloadedState。

即替换(在商店 index.js)

const createStoreWithMiddleware = applyMiddleware(reduxThunk)(createStore);
const store = createStoreWithMiddleware(reducers);

const store = createStore(reducers, {fetch:[]}, applyMiddleware(reduxThunk))

【讨论】:

  • 我得到了它的工作,我会更新我的组件让你看看我做了什么。无论如何谢谢!
  • 查看我的组件 trabalhos.js
  • 这与您的组件、动作、reducer 或 rootReducer 无关。这是关于你的 redux 商店你没有发布的代码。
  • 我已经发布了。更新 !!谢谢!
  • 但它不像你的答案那样工作,我不知道你的解决方案是否有效。
猜你喜欢
  • 2019-12-15
  • 2017-12-30
  • 2020-08-18
  • 2017-03-14
  • 2016-08-19
  • 2023-04-07
  • 2020-04-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多