Example of state records set:
export interface GenericStateRecord {
id: string;
[key: string]: unknown;
}
export interface State {
keys: GenericStateRecord[];
}
const state: State = {
keys: [
{
id: 'uniquestring',
type: 'AAA'
}
]
};
Adds adding of new record.
const dispatch = useAppDispatch();
dispatch({
type: 'SET_RECORD',
payload: {
name: 'keys',
record: {
id: 'anotheruniquestring',
type: 'ZZZ'
}
}
});
console.log(state);
/*
{
keys: [
{
id: 'uniquestring',
type: 'AAA'
},
{
id: 'anotheruniquestring',
type: 'ZZZ'
}
]
}
*/
Updating existing record (uniqueness by id
):
dispatch({
type: 'SET_RECORD',
payload: {
name: 'keys',
record: {
id: 'uniquestring',
type: 'ZZZ'
}
}
});
console.log(state);
/*
{
keys: [
{
id: 'uniquestring',
type: 'ZZZ'
}
]
}
*/
dispatch({
type: 'REMOVE_RECORD',
payload: {
name: 'keys',
id: 'uniquestring'
}
});
console.log(state);
/*
{
keys: []
}
*/
Reducer fails when:
payload.name
not providedpayload.record
not provided (SET_RECORD case)payload.record.id
not provided (SET_RECORD case)payload.id
not provided (REMOVE_RECORD case)