Reactjs 不发送的操作

Reactjs 不发送的操作,reactjs,redux,react-redux,redux-thunk,Reactjs,Redux,React Redux,Redux Thunk,当我运行action creator创建thunk时,它不会运行分派函数 为了进行测试,我在我的根组件的渲染方法中放置了一个按钮,我的根组件被连接在一起(删除了无关的东西): 我的减速机是骨库,看起来像这样: export function loadAndSetCurrentUser(username) { console.log("LASCU: " + username); return (dispatch, getState) => { console.

当我运行action creator创建thunk时,它不会运行分派函数

为了进行测试,我在我的根组件的渲染方法中放置了一个按钮,我的根组件被连接在一起(删除了无关的东西):

我的减速机是骨库,看起来像这样:

export function loadAndSetCurrentUser(username) {
    console.log("LASCU: " + username);
    return (dispatch, getState) => {
        console.log("in dispatch");
        dispatch(this.requestingUserData(username)); 

        const user = getState().Users[username];
        if (user) {
            // if already in store, just change the current username  
            //FUTURE: check date against user mod date in database
            dispatch(setCurrentUsername(username));
            dispatch(userDataLoadComplete());
        } else {
            // user not in store, need to check database //TODO:
            fetch('https://jsonplaceholder.typicode.com/users?username=Bret')
                .then(response => response.json())
                // .then(json => delay(3000, json))
                .then(json=> dispatch(ingestUserData(json)))
                .then(() => dispatch(userDataLoadComplete()));
        }
    }
}

export function requestingUserData(username) {
    console.log("RUD");
    return { type: REQUESTING_USER_DATA, username };
}

export function setCurrentUsername(username) {
    return { type: SET_CURRENT_USERNAME, username }
}

export function ingestUserData(userData) {
    return (dispatch) =>
        {
            console.log("IUD");
            console.log(userData);
            dispatch({ type: SET_USER_DATA, userData })
        }
}

export function userDataLoadComplete() {
    return { type: USER_DATA_LOAD_COMPLETE };
}
export function SedFF(state = initialSedFFState, action) {
    let newState = _.cloneDeep(state);
    switch (action.type) {
        case SET_CURRENT_USERNAME:
            newState.currentUsername = action.username
            return newState;
        case LOAD_USER_DATA:
            //TODO: 
             return newState;
        case REQUESTING_USER_DATA:
            newState.isFetchingUserData = true;
            return newState;
        case RECEIVED_USER_DATA:
            //TODO:
            newState.isFetchingUserData = false;
            return newState
        case USER_DATA_LOAD_COMPLETE:
            //TODO:
            newState.isFetchingUserData = false;
            return newState

... etc, etc...

        default:
            return state
    }
}
当我点击LASCU按钮时,我得到以下输出:
LASCU:username@email.com
来自我的action creator(在上面action creator文件的第二行注明)。我的action creator文件第4行的dispatch输出中没有
。我没有任何行动

我不确定为什么调度没有开火

我会注意到,在另一个组件(子组件)上,我可以让它触发整个事件,但我找不到任何差异。唯一真正的区别似乎是我的导出行中没有路由器:
导出默认连接(mapStateToProps、mapDispatchToProps)(带有样式(样式)(系统菜单))

值得一提的是,我的thunk中间件连接在App.js(我的根组件的父级)上,如下所示:

const store=createStore(
减根剂,
初始状态,
composeEnhancer(applyMiddleware(thunk))
);
类应用程序扩展了React.Component{
render(){
返回(
);
}
}
使用样式导出默认值(样式,{withTheme:true})(应用程序);

想法?帮助?

您仍然必须发送Thunk操作,否则它只会返回操作,而不会对其执行任何操作

render() {
    return (
        <button onClick={() => 
            store.dispatch(loadAndSetCurrentUser("username@email.com"))
        }>LASCU</button>
    )
}

看看问题中给出的render方法,问题似乎是您调用的是原始导入函数,而不是props中的绑定函数。换句话说,更改为
this.props.loadAndSetCurrentUser()
应该可以正常工作。请注意


有关更多详细信息,请参阅React Redux。

您很接近,但有点偏离。看看问题中给出的render方法,问题似乎是OP调用的是原始导入的函数,而不是props中的绑定函数。换句话说,更改为
this.props.loadAndSetCurrentUser()
应该可以正常工作。克里斯蒂:一般来说,我不能直接进入商店@马克里克森:你完全正确。我为你做了改变,它成功了。将其转换为答案,我会将其标记为正确。@markerikson,请稍候,如果您使用映射/绑定函数,它会在调用构造函数后自动分派操作吗?(我的解决方案可能仍然有效,但这不是推荐的做法,事实上我在生产中使用它已经很长时间了,而且没有任何问题,有一个单件商店)是的。我所做的只是在我的电话前加上“这个。道具”,它就起作用了。这是有意义的,因为现在它正在处理通过mapDispatchToProps传递的函数,而不是直接从导入调用函数(正如我错误地做的那样)。你说得对,我认为你的解决方案也会奏效。@Cristy:是的,这就是
mapDispatchToProps
:)的全部要点。有关详细信息,请参阅React Redux。
const store = createStore(
    RootReducer, 
    initialState, 
    composeEnhancer(applyMiddleware(thunk))
);

class App extends React.Component {

    render() {
        return (
            <Provider store={store}>
                <CssBaseline />
                <BrowserRouter>
                    <WebFF />
                </BrowserRouter>
            </Provider>
        );
    }
}


export default withStyles(styles, { withTheme: true })(App);
render() {
    return (
        <button onClick={() => 
            store.dispatch(loadAndSetCurrentUser("username@email.com"))
        }>LASCU</button>
    )
}