score:314
ESLint doesn't like let statements inside case blocks inside a reducer, Why?
This is discouraged because it results in the variable being in scope outside of your current case
. By using a block you limit the scope of the variable to that block.
Use {}
to create the block scope with case, like this:
case DELETE_INTEREST: {
let .....
return (...)
}
Check this snippet:
function withOutBraces() {
switch(1){
case 1:
let a=10;
console.log('case 1', a);
case 2:
console.log('case 2', a)
}
}
function withBraces() {
switch(1){
case 1: {
let a=10;
console.log('case 1', a);
}
case 2: {
console.log('case 2', a)
}
}
}
console.log('========First Case ============')
withOutBraces()
console.log('========Second Case ============')
withBraces();
For deleting the element from array, use array.filter, because splice will do the changes in original array. Write it like this:
case DELETE_INTEREST:
let deleteInterests = state.user.interests;
let newData = deleteInterests.filter(i => i !== action.payload);
return { ...state, user: { ...state.user, interests: newData } };
score:6
try to encapsulate the inside the case with {} like this look simple example
case EnumCartAction.DELETE_ITEM: {
const filterItems = state.cart.filter((item) => item._id !== action.payload)
return {
...state,
cart: filterItems
}
}
Source: stackoverflow.com
Related Query
- eslint: no-case-declaration - unexpected lexical declaration in case block
- Eslint Error - Unexpected block statement surrounding arrow body; move the returned value immediately after the =>
- ESLint React error Unexpected block statement surrounding arrow body
- Unexpected block statement surrounding arrow body ESlint
- Unexpected block statement surrounding arrow body with ESLint
- React JS esLint error Unexpected block statement surrounding arrow body; move the returned value immediately after the =>
- ESLint error Unexpected block statement surrounding arrow body
- react filter eslint error: unexpected block statement surrounding arrow body
- ESLint Parsing error: Unexpected token
- Cannot fix eslint rule on indenting case statements in switch statement
- Unexpected block statement surrounding arrow body
- eslint "parsing error: Unexpected token {" in JSX
- eslint Parsing error: Unexpected token =
- How can I fix ESLint Parsing Error for unexpected token "/"?
- ESLint simple react App.js Unexpected newline after parentheses function-paren-newline
- eslint throwing import/no-unresolved error on declaration files
- ESLint Parsing error: Unexpected token
- react/jsx-a11y eslint throws unexpected errors for control/label
- Unexpected token <> (React Fragment) error from ESLint
- React Router Link params. Unexpected character '`'. in uglify and eslint
- ESLint has parsing error Unexpected token /
- React - Eslint - Camel camel case props
- Can't access lexical declaration `callback' before initialization
- block eslint and prettier from reformatting closing brackets on HTML elements
- Eslint Parsing error: Unexpected token
- Jest test case gives Unexpected token in dynamic import() error
- ReferenceError: can't access lexical declaration 'useStyles' before initialization React Collapse Component, axios get data material ui useStyles
- ReactJS tutorial (tic tac) creating block of HTML unexpected token error
- ESLINT Unexpected top-level property "import/order" error
- eslint: Unexpected block statement surrounding arrow body
More Query from same tag
- Since setState is asynchronous, is it executed via callback queue?
- The value of the checkbox is added to the array but the checkbox is not checked
- I'm having trouble loading an .svg image in react using styled components. Can anyone tell me why its not loading?
- React Native: Create custom child node in firebase realtime database
- Difference between component and container in react redux
- TypeError: Cannot read property 'props' of undefined. Need to pass props form a functional component to another one
- nestedItems[0] of type `array` supplied to `ListItem` expected a single ReactElement
- webpack-dev-server Cannot find module 'webpack'
- How to grab params from URL in react-router v 2.+
- Axios result undefined inside render() method in React
- First time using React-Transition-Group
- why d3.path().moveTo() function is undefined?
- How to authenticate using JWT in reactjs stored in localstorage on refresh page?
- reactjs modal not showing
- Change localization directory base on local storage item
- Best way to implement modals and notifications in React Flux
- TypeError: Cannot set property 'state' of undefined
- React hook use class object as useState
- Why useDispatch re-renders parent components?
- Reactjs Timezone convertion
- React auth with localStorage
- How to configure Stylus support in a React.js application?
- Why is the scrollheight of my React component in an iframe 150px?
- How to do word-wrap for data using react-table?
- How to update input value using ref in ReactJS
- react-csv: downloaded csv breaks tab after " "
- Added 2 Schema in Yup Validation React-Hook-Form
- Is there any way to change where Material-UI adds style tag html elements?
- How can I 'await' setState update or something to the same effect?
- Using nginx to run dynamic content