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;
}
}
};
Вот, в итоге что я имею.