scrimba
Reusable React
Reducers
Why are reducers a part of React?
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Why are reducers a part of React?
AboutCommentsNotes
Why are reducers a part of React?
Expand for more info
index.js
run
preview
console
import React, { useState, useReducer } from 'react';
import ReactDOM from 'react-dom';

const App = () => {
const [count, setCount] = useState(0)

const add = () => {
setCount(count + 1)
}

const subtract = () => {
if (count > 0) {
setCount(count - 1)
}
}

return (
<section>
<h2>Counter: The Most Novel Example I Could Come Up With</h2>
<div className="counter">
<button onClick={subtract}>-</button>
<input
type="text"
aria-label="count"
defaultValue={count}
/>
<button onClick={add}>+</button>
</div>
</section>
)
}
ReactDOM.render(<App />, document.getElementById('root'));
Console
/index.html?
-5:04