简体   繁体   English

cb && 'function' === typeof cb && cb(data) 如何在带有 redux-thunk 的 Action(React-redux) 中工作?

[英]how does cb && 'function' === typeof cb && cb(data) work in the Action(React-redux) with redux-thunk?

I create a mock promise as delay()to access redux-thunk.我创建了一个模拟 promise 作为 delay() 来访问 redux-thunk。

In the action part, I could not understand cb && 'function' === typeof cb && cb(data) and how does it work in the action with redux-thunk?在动作部分,我无法理解cb && 'function' === typeof cb && cb(data)以及它在使用 redux-thunk 的动作中如何工作?

class Actions {
    static start() {
        return {
            type: actionType.CREATE_TODO_DOING
        }
    }

    static ok(data, cb) {
        cb && 'function' === typeof cb && cb(data);
        return {
            type: actionType.CREATE_TODO_SUCCESS,
            payload: data
        }
    }

    static fail(data, cb) {
        cb && 'function' === typeof cb && cb(data);
        return {
            type: actionType.CREATE_TODO_FAILURE,
            payload: data
        }
    }
}

export default (data, cb) => {
    return (dispatch, getState) => {
    dispatch(Actions.start());
        delay(0.5).then(() => {
            dispatch(Actions.ok(data, cb));
        }).catch(error => dispatch(Actions.fail(error || 'Create failed', cb)))
    }
}

It is a shortcut for these这是这些的捷径

if (cb && 'function' === typeof cb) { // check cb has value + cb is a function or not
  cb(data); // call cb function
}

And it just a callback function that you want to call it when Actions.ok function is called, that's it它只是一个回调 function 你想在Actions.ok function 被调用时调用它,就是这样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM