useReducer example
/*
A common use case is to access a child imperatively:
*/
function TextInputWithFocusButton() {
const inputEl = useRef(null);
const onButtonClick = () => {
// `current` points to the mounted text input element
inputEl.current.focus();
};
return (
<>
<input ref={inputEl} type="text" />
<button onClick={onButtonClick}>Focus the input</button>
</>
);
}
4.67
6
function init(initialCount) { return {count: initialCount};}
function reducer(state, action) {
switch (action.type) {
case 'increment':
return {count: state.count + 1};
case 'decrement':
return {count: state.count - 1};
case 'reset': return init(action.payload); default:
throw new Error();
}
}
function Counter({initialCount}) {
const [state, dispatch] = useReducer(reducer, initialCount, init); return (
<>
Count: {state.count}
<button
onClick={() => dispatch({type: 'reset', payload: initialCount})}> Reset
</button>
<button onClick={() => dispatch({type: 'decrement'})}>-</button>
<button onClick={() => dispatch({type: 'increment'})}>+</button>
</>
);
}
Thank you!
6
0
3.8
5
const [state, dispatch] = useReducer(
reducer,
{count: initialCount} );
Thank you!
5
0
4.67
3
import React, { useState, useReducer } from "react";
import ReactDOM from "react-dom";
import "./styles.css";
function reducer(state, action) {
switch (action.type) {
case "add":
return [...state, action.item];
case "remove":
return [
...state.slice(0, action.index),
...state.slice(action.index + 1)
];
default:
throw new Error();
}
}
function FavoriteMovies() {
const [movies, dispatch] = useReducer(reducer, [{ name: "Heat" }]);
const [newMovie, setNewMovie] = useState("");
const handleAddClick = () => {
if (newMovie === "") {
return;
}
dispatch({ type: "add", item: { name: newMovie } });
setNewMovie("");
};
return (
<>
<div className="movies">
{movies.map((movie, index) => {
return (
<Movie
movie={movie}
onRemove={() => dispatch({ type: "remove", index })}
/>
);
})}
</div>
<div className="add-movie">
<input
type="text"
value={newMovie}
onChange={event => setNewMovie(event.target.value)}
/>
<button onClick={handleAddClick}>Add movie</button>
</div>
</>
);
}
function Movie({ movie, onRemove }) {
return (
<div className="movie">
<span>{movie.name}</span>
<button onClick={onRemove}>Remove</button>
</div>
);
}
function App() {
return (
<div className="App">
<h2>My favorite movies</h2>
<FavoriteMovies />
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Thank you!
3
0
3.57
7
const [state, dispatch] = useReducer(reducer, initialArg, init);
Thank you!
7
0
0
5
const initialState = {count: 0};
function reducer(state, action) {
switch (action.type) {
case 'increment':
return {count: state.count + 1};
case 'decrement':
return {count: state.count - 1};
default:
throw new Error();
}
}
function Counter() {
const [state, dispatch] = useReducer(reducer, initialState);
return (
<>
Count: {state.count}
<button onClick={() => dispatch({type: 'decrement'})}>-</button>
<button onClick={() => dispatch({type: 'increment'})}>+</button>
</>
);
}
Thank you!
5
0
Are there any code examples left?
New code examples in category Javascript
-
Javascript 2023-04-21 22:52:36
-
Javascript 2022-03-27 22:25:20 javascript download string as file
-
Javascript 2022-03-27 21:40:22 sort numbers in array javascript
-
Javascript 2022-03-27 21:20:04 compare two arrays and return the difference javascript
-
Javascript 2022-03-27 21:15:02 javascript regex french phone number
-
Javascript 2022-03-27 21:05:03 cypress custom error message
-
Javascript 2022-03-27 21:00:05 create element javascript with id
-
Javascript 2022-03-27 20:40:57 feather icons react
-
Javascript 2022-03-27 20:20:01 how to make graphql request in axios
-
Javascript 2022-03-27 20:15:07 bootstrap validator password and confirm password