EN
React - handleClick switch error
1 answers
0 points
Do you know why my handleClick
function throws an uncaught error?
my code:
xxxxxxxxxx
1
// Note: Uncomment import lines during working with JSX Compiler.
2
// import React from 'react';
3
// import ReactDOM from 'react-dom';
4
5
function App() {
6
const [state, setState] = React.useState(0);
7
8
function handleClick(type) {
9
switch (type) {
10
case 'increment':
11
setState((state) => state + 1);
12
case 'decrement':
13
setState((state) => state - 1);
14
default:
15
throw new Error(type);
16
}
17
}
18
19
return (
20
<>
21
Count: {state}
22
<button onClick={() => handleClick('increment')}>+</button>
23
<button onClick={() => handleClick('decrement')}>-</button>
24
</>
25
);
26
}
27
28
const root = document.querySelector('#root');
29
ReactDOM.render(<App/>, root );
stacktrace:
xxxxxxxxxx
1
App.js:19 Uncaught Error: increment
2
at handleClick (App.js:19)
3
at onClick (App.js:26)
4
at HTMLUnknownElement.callCallback (react-dom.development.js:3945)
5
at Object.invokeGuardedCallbackDev (react-dom.development.js:3994)
6
at invokeGuardedCallback (react-dom.development.js:4056)
7
at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:4070)
8
at executeDispatch (react-dom.development.js:8243)
9
at processDispatchQueueItemsInOrder (react-dom.development.js:8275)
10
at processDispatchQueue (react-dom.development.js:8288)
11
at dispatchEventsForPlugins (react-dom.development.js:8299)
12
at react-dom.development.js:8508
13
at batchedEventUpdates$1 (react-dom.development.js:22396)
14
at batchedEventUpdates (react-dom.development.js:3745)
15
at dispatchEventForPluginEventSystem (react-dom.development.js:8507)
16
at attemptToDispatchEvent (react-dom.development.js:6005)
17
at dispatchEvent (react-dom.development.js:5924)
18
at unstable_runWithPriority (scheduler.development.js:646)
19
at runWithPriority$1 (react-dom.development.js:11276)
20
at discreteUpdates$1 (react-dom.development.js:22413)
21
at discreteUpdates (react-dom.development.js:3756)
22
at dispatchDiscreteEvent (react-dom.development.js:5889)
1 answer
0 points
I found the solution to the problem.
I didn't include break
in any of the cases, so all the cases below were executed, including throwing an error.
Here's the solution:
xxxxxxxxxx
1
// Note: Uncomment import lines during working with JSX Compiler.
2
// import React from 'react';
3
// import ReactDOM from 'react-dom';
4
5
function App() {
6
const [state, setState] = React.useState(0);
7
8
function handleClick(type) {
9
switch (type) {
10
case 'increment':
11
setState((state) => state + 1);
12
break;
13
case 'decrement':
14
setState((state) => state - 1);
15
break;
16
default:
17
throw new Error(type);
18
}
19
}
20
21
return (
22
<>
23
Count: {state}
24
<button onClick={() => handleClick('increment')}>+</button>
25
<button onClick={() => handleClick('decrement')}>-</button>
26
</>
27
);
28
}
29
30
const root = document.querySelector('#root');
31
ReactDOM.render(<App/>, root );
0 commentsShow commentsAdd comment