Сообщество IT-специалистов
Ответы на любые вопросы об IT
Профессиональное развитие в IT
Удаленная работа для IT-специалистов
const mapStateToProps = (state) => ({state });
const Counter = ({ actionRed,actionGreen, store }) => {
import React from "react"; import { connect } from "react-redux"; import { actionGreen, actionRed } from "../../actions/counter"; const Counter = ({ actionRed,actionGreen, state }) => { return ( <div> <button onClick={actionRed} >Red</button> <button onClick={actionGreen} >Green</button> </div> ); } const mapStateToProps = (state) => (state = { background: "red" }); const mapDispatchToProps = { actionRed, actionGreen }; export const CounterContainer = connect(mapStateToProps, mapDispatchToProps)(Counter); ========= const initialColor ={ background: 'white' } const initialRed ={ background: 'red' } const initialGreen ={ background: 'green' } export const counterReducer = (state=initialColor, action) => { switch(action.type) { case "RED": { return { state:initialRed }; } case "GREEN": { return { state:initialGreen.background } } default: { return state; } } };
import React from "react"; import { connect } from "react-redux"; import { actionGreen, actionRed } from "../../actions/counter"; const Counter = ({ actionRed,actionGreen, state }) => { return ( <div> <button onClick={actionRed} style={{backgroundColor: state}}>Red</button> <button onClick={actionGreen} style={{backgroundColor: state}}>Green</button> </div> ); } const mapStateToProps = (state) => ({state }); const mapDispatchToProps = { actionRed, actionGreen }; export const CounterContainer = connect(mapStateToProps, mapDispatchToProps)(Counter); ========= const initialColor ={ background: 'white' } const initialRed ={ background: 'red' } const initialGreen ={ background: 'green' } export const counterReducer = (state=initialColor.background, action) => { switch(action.type) { case "RED": { return { state:initialRed.background }; } case "GREEN": { return { state:initialGreen.background } } default: { return state; } } };
import {GREEN, RED} from "../constants"; export const actionRed = () => ({ type: RED }); export const actionGreen = () => ({ type: GREEN }); ======================== import { createStore } from "redux"; import { counterReducer } from "../reducers/counter"; export const store = createStore(counterReducer); ============== import React from "react"; import { connect } from "react-redux"; import { actionGreen, actionRed } from "../../actions/counter"; const Counter = ({ actionRed,actionGreen, state }) => { return ( <div> <button onClick={actionRed} style={{backgroundColor: state}}>Red</button> <button onClick={actionGreen} style={{backgroundColor: state}}>Green</button> </div> ); } const mapStateToProps = (state) => ({state }); const mapDispatchToProps = { actionRed, actionGreen }; export const CounterContainer = connect(mapStateToProps, mapDispatchToProps)(Counter); ============ const initialColor ={ background: 'white' } const initialRed ={ background: 'red' } const initialGreen ={ background: 'green' } export const counterReducer = (state=initialColor.background, action) => { switch(action.type) { case "RED": { return { state:initialRed.background }; } case "GREEN": { return { state:initialGreen.background } } default: { return state; } } };