我有這些文件,當我派遣的東西時,總是返回reducer的默認情況。React-Redux |調度員不工作
這是我第一次使用終極版/ thunk的,而我下面這個教程:https://www.youtube.com/watch?v=nrg7zhgJd4w當他這樣做,它的工作原理..
請看看我的代碼:
反應成分:
import React, { Component } from 'react';
import './App.css';
import Request from 'request';
import { connect } from 'react-redux'
import * as OperationsActions from './actions/operationsReducerActions'
//import { Content, FilterSelect, ListItem, Searchbar, Sidebar} from './components/index.js'
function mapStateToProps(state){
return {
operations : state.operations
}
}
class App extends Component {
constructor(props){
super(props);
}
componentDidMount(){
this.props.dispatch(OperationsActions.getOperations());
}
render() {
console.log(this.props)
return(
<div>{this.props.operations.operations[0].text}</div>
)
}
}
export default connect(mapStateToProps)(App)
行動文件:
import Request from 'request';
export function getOperations(){
Request('http://localhost:8000/client/REQUEST_OPERATIONS', (error, response, data) => {
if(!error){
return {type : 'FETCH_OPERATIONS_SUCCES', payload : data};
}
else {
return {type : 'FETCH_OPERATIONS_REJECTED', payload : error}
}
});
}
減速機:
export default function reducer(state={
operations : [{
text : '',
reqArgument : '',
filters : [],
url : ''
}],
fetching : false,
fetched : false,
error : null
}, action) {
switch(action.type){
case 'FETCH_OPERATIONS':{
return {...state, fetching : true }
}
case 'FETCH_OPERATIONS_REJECTED':{
return {...state, fetching : false, error : action.payload}
}
case 'FETCH_OPERATIONS_SUCCES':{
return {...state, fetched : true, fetching : false, operations : action.payload }
}
default : {
return {...state}
}
}
}
和我的商店:
進口{applyMiddleware,createStore}從 '終極版'
import logger from 'redux-logger'
import thunk from 'redux-thunk'
import promise from 'redux-promise-middleware'
import reducer from './reducers'
const middleware = applyMiddleware(promise, thunk, logger)
export default createStore(reducer, middleware)
什麼不工作是什麼呢?如果可以,請嘗試突出顯示問題。屏幕上什麼也沒有顯示出來?你有沒有嘗試用'console.log()'記錄任何值? –
是的,我試過控制檯日誌記錄的東西,問題是,我的減速器總是不斷回饋默認情況/狀態,或者我的調度員不能正常工作... –