Skip to content

Prefer Constant Dispatch

Suggestion
small-rules/prefer-constant-dispatch

Disallow inline useReducer action objects that could be module-level constants.

Diagnostic Messages

preferConstantDispatch
Move this inline useReducer action object to a module-level constant.

Configuration

This rule does not accept options.

{
"jsPlugins": [
"@pobammer-ts/small-rules"
],
"rules": {
"small-rules/prefer-constant-dispatch": "error"
}
}

Examples

Inline constant reducer action
function Component() {
const [state, dispatch] = useReducer(reducer, initialState);
dispatch({ type: "OPEN" });
return state;
}
Dynamic reducer action type
function Component(type) {
const [state, dispatch] = useReducer(reducer, initialState);
dispatch({ type });
return state;
}