【问题标题】:React-Redux - Fetching data from FirebaseReact-Redux - 从 Firebase 获取数据
【发布时间】:2016-12-14 14:15:39
【问题描述】:

我正在尝试使用 Firebase 来捕获一些数据并在我的网络应用程序上显示,但我不知道该怎么做。我正在使用 reduxThunk。 我收到错误id is not defined。 这是我的组件

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);

这是我的行动

actions/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;

【问题讨论】:

  • Trabalhos 组件在哪里设置它的 props?你打算让它成为一个连接的组件吗?
  • 我更新了!!
  • @Hseleiro 该错误来自哪里?您的 Firebase 应用程序是否正常工作?从文档看来,您必须在 Firebase 处于活动状态之前运行 initializeApp。此外,您必须在firebase.database().ref() 上致电.on() 以收听更改。我认为您不能直接在 firebase 实例上调用 .on()(链接如下)。 firebase.google.com/docs/reference/js/firebasefirebase.google.com/docs/database/web/read-and-write
  • @Shane 感谢您的帮助,当我运行 initializeApp 时出现错误:Uncaught TypeError: firebase.initializeApp is not a function(...)

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


【解决方案1】:

您在没有任何参数的情况下调用this.renderList(),所以id&lt;li className="list-group-item" key={id}&gt; 确实是is not defined。我想也许你想使用this.props.payload.data.map(this.renderList)

【讨论】:

  • this.props.payload.data.map(this.renderList) 对您正在尝试做的事情有预感。您应该用正确的道具名称替换。
  • 感谢 leo,但它不起作用,我正在尝试使用 _ lodash
猜你喜欢
  • 2018-11-07
  • 1970-01-01
  • 2021-09-27
  • 2020-11-04
  • 1970-01-01
  • 1970-01-01
  • 2021-02-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多