React Redux-从Firebase获取数据
我试图使用Firebase捕捉一些数据并显示在我的web应用程序上,但我不知道如何做到这一点。我用的是reduxThunk。 我收到的错误未定义id。 这是我的组件 trabalhos.jsReact Redux-从Firebase获取数据,firebase,firebase-realtime-database,redux,react-redux,redux-thunk,Firebase,Firebase Realtime Database,Redux,React Redux,Redux Thunk,我试图使用Firebase捕捉一些数据并显示在我的web应用程序上,但我不知道如何做到这一点。我用的是reduxThunk。 我收到的错误未定义id。 这是我的组件 trabalhos.js import React, { Component } from 'react'; import { connect } from 'react-redux'; import * as actions from '../actions'; class Trabalhos extends Componen
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);
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()
});
});
}
}
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'));
import { FETCH_DATA } from '../actions/types';
export default function(state = [], action) {
switch(action.type){
case FETCH_DATA:
return action.payload.data;
}
return state;
}
import { combineReducers } from 'redux';
import fetchReducer from './fetch_reducer';
const rootReducer = combineReducers({
fetch: fetchReducer
});
export default rootReducer;
这是我的索引.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);
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()
});
});
}
}
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'));
import { FETCH_DATA } from '../actions/types';
export default function(state = [], action) {
switch(action.type){
case FETCH_DATA:
return action.payload.data;
}
return state;
}
import { combineReducers } from 'redux';
import fetchReducer from './fetch_reducer';
const rootReducer = combineReducers({
fetch: fetchReducer
});
export default rootReducer;
这是我的Reducerindex.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);
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()
});
});
}
}
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'));
import { FETCH_DATA } from '../actions/types';
export default function(state = [], action) {
switch(action.type){
case FETCH_DATA:
return action.payload.data;
}
return state;
}
import { combineReducers } from 'redux';
import fetchReducer from './fetch_reducer';
const rootReducer = combineReducers({
fetch: fetchReducer
});
export default rootReducer;
您正在调用
this.renderList()
,而没有任何参数,因此id
在
确实没有定义。我想你可能想使用this.props.payload.data.map(this.renderList)
?你调用的this.renderList()
没有任何参数,所以id
在
实际上没有定义。我猜您可能想使用this.props.payload.data.map(this.renderList)
?Trabalhos组件的道具设置在哪里?你想让它成为一个连接的组件吗?我已经更新了@希莱罗这个错误是从哪里来的?你的firebase应用程序工作正常吗?从文档中可以看出,在Firebase激活之前,您必须运行initializeApp。此外,您必须在firebase.database().ref()
上调用.on()
,以侦听更改。我认为您不能直接在firebase实例上调用.on()
(下面的链接)@Shane感谢您的帮助,当我运行initializeApp时,我得到了一个错误:Uncaught TypeError:firebase.initializeApp不是一个函数(…),Trabalhos组件在哪里设置道具?你想让它成为一个连接的组件吗?我已经更新了@希莱罗这个错误是从哪里来的?你的firebase应用程序工作正常吗?从文档中可以看出,在Firebase激活之前,您必须运行initializeApp。此外,您必须在firebase.database().ref()
上调用.on()
,以侦听更改。我认为您不能直接在firebase实例上调用.on()
(下面的链接)@Shane感谢您的帮助,当我运行initializeApp时,我得到了一个错误:Uncaught TypeError:firebase.initializeApp不是一个函数(…)this.props.payload.data.map(this.renderList)
是关于您正在尝试做什么的预感。您应该用正确的道具名称替换。谢谢leo,但它不起作用,我尝试使用uLodashthis.props.payload.data.map(this.renderList)
是关于您尝试做什么的预感。你应该用正确的道具名称来替换。谢谢利奥,但它不起作用,我正在尝试使用