Created
June 11, 2025 13:56
-
-
Save sunmeat/7f29019605a5dec8d0f746f82e50b598 to your computer and use it in GitHub Desktop.
заготовка простейшего примера на редакс
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createStore } from 'redux'; | |
import { Provider, useSelector, useDispatch } from 'react-redux'; | |
// npm install redux react-redux | |
// редьюсер для счетчика | |
const counterReducer = (state = { count: 0 }, action) => { | |
switch (action.type) { | |
case 'INCREMENT': | |
return { count: state.count + 1 }; | |
case 'DECREMENT': | |
return { count: state.count - 1 }; | |
default: | |
return state; | |
} | |
}; | |
// создание хранилища | |
const store = createStore(counterReducer); | |
// компонент счетчика | |
function Counter() { | |
const count = useSelector((state) => state.count); // получение состояния | |
const dispatch = useDispatch(); // функция отправки действий | |
return ( | |
<div> | |
<h1>Счетчик: {count}</h1> | |
<button onClick={() => dispatch({ type: 'INCREMENT' })}>+</button> | |
<button onClick={() => dispatch({ type: 'DECREMENT' })}>-</button> | |
</div> | |
); | |
} | |
// обертка приложения | |
function App() { | |
return ( | |
<Provider store={store}> | |
<Counter /> | |
</Provider> | |
); | |
} | |
export default App; | |
сделать цсс |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment