【发布时间】:2016-12-15 17:19:46
【问题描述】:
我正在使用 react、redux 和 redux-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