Node.jsがインストール済であること
yarn add redux react-redux
これでまずreduxを使用できるようにすること
// ActionCreater
// Componentで使用される
export const INCREMENT = "INCREMENT"
export const DECREMENT = "DECREMENT"
export const increment = () => ({
type: INCREMENT
})
export const decrement = () => ({
type: DECREMENT
})
import {INCREMENT, DECREMENT} from "../Actions/"
const initialState = {value: 0}
export default(state = initialState, action) => {
switch(action.type){
case INCREMEMT:
return {value: state.value + 1};
case DECREMENT:
return {alue: state.value - 1}
}
}
import React from 'react';
import ReactDOM from 'react-dom';
import {createStore} from 'redux';
import {Provider} from 'react-redux';
import './index.css';
import App from './Components/App';
import reducer from 'Reducers';
import reportWebVitals from './reportWebVitals';
const store = createStore(reducer)
ReactDOM.render(
<React.StrictMode>
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>,
document.getElementById('root')
);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: <https://bit.ly/CRA-vitals>
reportWebVitals();
import {createStore} from 'redux';
import {Provider} from 'react-redux';
import reducer from 'Reducers';
上記import後、
const store = createStore(reducer)
ReactDOM.render(
<React.StrictMode>
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>,
document.getElementById('root')
);
createStoreメソッドを使用