React-Redux - 从Firebase获取数据

时间:2016-12-14 14:15:39

标签: firebase firebase-realtime-database redux react-redux redux-thunk

我尝试使用Firebase捕获一些数据并在我的网络应用上显示,但我不明白如何做到这一点。我正在使用reduxThunk。 我收到错误 id未定义。 这是我的组件

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({id,tec,title}){
        return(
            <li className="list-group-item" key={id}>
                <p>{title}</p>
                <p>{tec}</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';

const Data = new Firebase('https://portofoliofirebase.firebaseio.com');

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

}

这是我的 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'));

这是我的 fetch_reducer.js

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

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

return state;
}

这是我的Reducer index.js

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

const rootReducer = combineReducers({

fetch: fetchReducer

});

export default rootReducer;

1 个答案:

答案 0 :(得分:0)

您在没有任何参数的情况下致电this.renderList(),因此id确实<li className="list-group-item" key={id}> is not defined。我想也许你想用this.props.payload.data.map(this.renderList)

相关问题