Open lebobbi opened 4 years ago
This would have pretty heavy consequences in our types, which are already too complicated and are just bound to get more complicated due to #637.
So for now, I'm not really seeing this make it into RTK, but with #637, you will be able to do things like
createSlice({
name,
initialState,
reducers: create => ({ // callback form
addTodo: create.preparedReducer(
(text: string) => ({payload: {text, id: nanoid()}}),
(state, action) => void state.push(action.payload);
)
})
})
so from that point on you could have a method like
function withPayloadAndMeta<P,M>(){
return function prepare(payload: P, meta: M) { return {payload, meta}; }
}
and use it like
createSlice({
name,
initialState,
reducers: create => ({
addTodo: create.preparedReducer(
withPayloadAndMeta<string, number>(),
(state, action) => void state.push(action.payload);
)
})
})
this would be a good use case for custom reducer creators (#3837):
const enhancedReducerCreator = {
type: enhancedReducerCreatorType,
create(reducer) {
return preparedReducerCreator.create(withPayloadAndMeta(), reducer)
}
}
createSlice({
name,
initialState,
creators: { enhancedReducer: enhancedReducerCreator },
reducers: (create) => ({
addTodo: create.enhancedReducer<string, number>(
(state, action) => void state.push(action.payload)
)
}),
})
The current implementation of the createSlice by default creates an actionCreator with the signature for a single parameter. In our application we are using the meta pretty much on every single action and sometimes the error properties of the flux standard actions as well. And while redux toolkit has been great to reduce boilerplate, having to override the action with prepare for every single case kind of fails this purpose. Is there a way we could add an extra property to the slice configuration where we could define the default signature for that slice generated action creators?
I was thinking something like this:
And in the implementation would be something like this:
Then the signature of the 'getAll' actionCreator would end up like this.