【发布时间】:2018-01-26 00:46:26
【问题描述】:
我有这些文件,不知何故,当我发送某些东西时,它总是返回减速器的默认情况。
这是我第一次使用 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}
}
}
}
还有我的商店:
从“redux”导入 { 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()记录任何值? -
是的,我尝试了控制台日志记录,但问题是我的减速器总是不断返回默认案例/状态,或者我的调度程序无法正常工作......
标签: javascript reactjs react-redux