没有从redux thunk

时间:2018-05-31 00:06:22

标签: reactjs react-redux redux-thunk reducers redux-actions

行动创作者

export function pickup(latlng) {

    return function(dispatch) {
        dispatch({ type: PICKUP_STATE,payload:latlng });
    };

}

减速机

import {
    PICKUP_STATE,
    PICKUP_ADD,
    DROPOFF_STATE
} from '../actions/types';

export default  (state={},action) => {
    const INITIAL_STATE = {
        pickup: '',
        pickupAdd:''
    };
    switch(action.type) {
        case PICKUP_STATE:
            console.log(action.payload)
            return {...state,pickup:action.payload};
        case PICKUP_ADD:
            return{...state,pickupAdd:action.payload};
        case DROPOFF_STATE:
            return {...state,dropoff:action.payload}
        default:
            return state;
    }
    //return state;
}

地图组件

import {
    connect
} from "react-redux";
import * as actions from "../actions"
class Map extends React.Component {
      componentWillReceiveProps(nextprops) {
             if (nextprops.pickupProps !== undefined) {
                 this.setState({
                     pick: nextprops.pickupProps
                 }, () => {
                     console.log(this.state.pick);

                 });
             }
       }
   isPickEmpty(emptyPickState) {
        this.props.pickup(emptyPickState);
    // setTimeout(() =>{ console.log('sdkjlfjlksd',this.state.pick) 
                        },3000);
        console.log(this.state.pick);
   }
}
const mapStateToProps = (state) => {
    // console.log(state.BookingData.pickup);
    return {
        pickupProps:state.BookingData.pickup,
        pickupAddProps: state.BookingData.pickupAdd
        }
}
export default connect(mapStateToProps,actions)(Map);

app.js 根文件

import React        from "react";
import ReactDOM     from "react-dom";
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import "normalize.css/normalize.css"
import  "./styles/styles.scss";
import { Router, Route, IndexRoute, browserHistory } from 'react-router';
import reduxThunk from 'redux-thunk';
import { composeWithDevTools } from 'redux-devtools-extension';

import AppRouter from './routers/AppRouter';
import reducers from './reducers';
import {AUTH_USER} from "./actions/types";


const middleware = [
    reduxThunk,
];

const store = createStore(reducers, composeWithDevTools(
    applyMiddleware(...middleware),
// other store enhancers if any
));



const token = localStorage.getItem('token');
if(token){
    store.dispatch({type:AUTH_USER});
}

ReactDOM.render(
    <Provider store={store}>
        <AppRouter />
    </Provider>
    , document.getElementById('app'));

这里我的问题是当我从我的地图组件中调用 isPickEmpty() 它会调用操作创建者 this.props.pickup(false)(我还在redux-devtools中检查了它显示 false 值)然后我安慰选择状态(存储在componentWillReceiveProps(nextprops)中),以便显示默认值而不是false 但是当我正在setTimeout(() =>{console.log('sdkjlfjlksd',this.state.pick) }, 3000);显示falsemodule.exports = { entry: { scss: ['./src/scss/custom.scss'] }, module: { rules: [ { test: /\.scss$/, loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: [{ loader: 'css-loader', options: { minimize: false, autoprefixer: true, sourceMap: true, importLoaders: 1 } }, 'postcss-loader', 'sass-loader'] }) } ] }, plugins: [ new ExtractTextPlugin({ filename: './assets/css/style.css', disable: false, allChunks: true }), ] } 内的值 纠正我,如果我错了我知道redux-thunks以同步方式工作而不是异步方式所以这里为什么它不能以同步方式工作
我被卡住了,任何人都帮帮我! 的更新
我刚刚得到了prblm的位置,实际上是在 componentWillReceiveProps ,其中我设置了pick状态值,因为它是异步的,所以当我在 isPickEmpty 函数中获取值时我是获得预期价值 如何处理setState或有什么方法可以解决

1 个答案:

答案 0 :(得分:0)

在组件中使用BookingData中的值,但在reducer上将其直接添加到状态。

const mapStateToProps = (state) => {
    console.log(state);//Check the state here
    return {
        pickupProps:state.pickup,
        pickupAddProps: state.pickupAdd
        }
}

如果你这个mapStateToProps

,应该可以正常工作