【问题标题】:React - Redux - Integrate Firebase Database in my AppReact - Redux - 在我的应用程序中集成 Firebase 数据库
【发布时间】:2016-12-15 17:19:46
【问题描述】:

我正在使用 reactreduxredux-thunk 制作一个简单的作品集。目标是从 firebase 捕获数据并将其显示在我的 trabalhos.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()
        });
    });
};
}

这是我的Reducer

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

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

return state;
}

这是我的 rootReducer

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

const rootReducer = combineReducers({

fetch: fetchReducer

});

export default rootReducer;

这是我的 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(
            <li>

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

谢谢!

【问题讨论】:

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


    【解决方案1】:

    首先是一个很好的视频,可以帮助您了解here

    现在您在对象调用 fetch 上有了 mapStateToProps。将名称更改为更易于理解的内容(例如数据等)会是一个更好的主意。但这是个人选择。

    function mapStateToProps(state){
    
    return { data: state.fetch };
    
    }
    

    现在您可以像访问所有内容一样访问组件中的道具。您使用 this.props.fetch 或者如果您更改名称 this.props.data 。我不知道您的数据是什么样的,但一个好用的方法是 react dev tools,因此很容易看到每个组件上的道具。

    最后一件事是做关于 redux 的教程here,这是来自 redux 创建者的免费和很棒的资源;)

    希望对你有帮助。

    【讨论】:

    • 感谢您的帮助!如果我控制我的操作,我可以看到来自数据库的信息,所以我在我的 reducer 中获取信息,现在困难的是在我的组件上显示该数据,我收到错误 - 未捕获的 TypeError:this.props。 fetch.map 不是函数(…)——你能帮帮我吗?
    • 肯定会导致数据不是数组而是对象。您需要 console.log 数据并找出处理这种类型的方法。在您的组件中只需 console.log(this.props) 并让我知道哪种数据是
    猜你喜欢
    • 2019-01-20
    • 2021-03-05
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 2020-09-20
    • 1970-01-01
    • 2017-08-20
    相关资源
    最近更新 更多