• Redux background как изменить?

    @Tom111 Автор вопроса
    К сожалению, так и не заработало. Вставил ваш код, но ничего не происходит. Ошибки тоже никакие не выдает.

    Вот, в итоге что я имею.

    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;
            }
        }
    };
  • Redux background как изменить?

    @Tom111 Автор вопроса
    Вот что у меня получилось. Но так и не заработало

    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;
            }
        }
    };
  • Redux background как изменить?

    @Tom111 Автор вопроса
    можете подсказать, как это реализовать?