I'm coding ( Slice ) from Redux, but I need the ( initialState ) to be NULL, what's the best way to do this? I tried inserting the comma but it didn't work.
My Code:
import { createSlice } from '@reduxjs/toolkit';
export const slice = createSlice({
name: 'initial',
initialState,
reducers: {
ADDS(state, actions) {
return {
...state,
product: actions.payload,
}
}
}
});
export const { ADDS } = slice.actions;
export default slice.reducer;
My Error:
Uncaught ReferenceError: initialState is not defined
at ./src/redux/slice.js (slice.js:6:1)
at options.factory (react refresh:6:1)
at __webpack_require__ (bootstrap:24:1)
at fn (hot module replacement:62:1)
at ./src/App.js (bundle.js:21:70)
at options.factory (react refresh:6:1)
at __webpack_require__ (bootstrap:24:1)
at fn (hot module replacement:62:1)
at ./src/index.js (UI.jsx:3:1)
at options.factory (react refresh:6:1)
Thanks
CodePudding user response:
Just set it to null?
const initialState = null;
export const slice = createSlice({
name: 'initial',
initialState,
CodePudding user response:
With ES6 shorthand notations let obj = {x};
is short for let obj = {x : x}
. So this will only work if x
is defined. Otherwise the code will throw an error since x
is undefined.
//let x = 2; //uncomment this to see
console.log({x});
You can do this:
export const slice = createSlice({
name: 'initial',
initialState : null,
reducers: {
ADDS(state, actions) {
return {
...state,
product: actions.payload,
}
}
}
});
Here is the doc for shorthand notations