Skip to content

记一个将数组作为函数参数传递的小技巧 #3

Description

@ibufu
const bar = [1, 2, 3];
foo(...bar);

等同于:

foo.apply(this, bar);

一个用例:

const middleware = [
    thunk,
    api,
    !__NODE__ && createLogger(),
].filter(Boolean);

export default function configureStore(initialState) {
    const store = createStore(
        rootReducer,
        initialState,
        compose(
            applyMiddleware(...middleware),
        )
    );

    return store;
}

redux 的 applyMiddleware 方法不能接受数组,可以利用这个方法方便地根据环境改变参数。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions