M
M
Michael R.2019-07-09 11:08:24
JavaScript
Michael R., 2019-07-09 11:08:24

ReactJS and combineReducers, what's wrong?

Greetings!
Without using combineReducers, using a single reducer - everything works fine. It was necessary to separate the reducers, used combineReducers, it seems to have done everything according to the instructions, but an error still occurs, the search for the error did not give any results.
HELP!
/src/index.js :

import React from "react";
import ReactDOM from "react-dom";
import {createStore} from "redux";
import {Provider} from "react-redux";

import reducersRoot from "./reducers";

const stateInitial = {count: 0}
const store = createStore(reducersRoot, stateInitial);

import AppCounter from "./AppCounter";

ReactDOM.render(
    <Provider store={store}>
        <AppCounter/>
    </Provider>,
    document.getElementById("root")
);

/src/AppCounter.js :
import React from "react";

import ContainerCounter from "./containers/Counter";

const AppCounter = () => (
    <ContainerCounter/>
);

export default AppCounter;

/src/reducers/index.js :
import {combineReducers} from "redux";

import reducerCounter from "./counter";

export default combineReducers({
    count: reducerCounter
});

/src/reducers/counter.js :
import * as actions from "../actions";

const counter = (state = {}, action) => {

    if(action.type === actions.COUNTER_DECREMENT) {
        return {count: state.count - action.count};

    } else if(action.type === actions.COUNTER_INCREMENT) {
        return {count: state.count + action.count};
    
    } else if(action.type === actions.COUNTER_RESET) {
        return {count: action.count};

    } return state;

}

export default counter;

/src/containers/Counter.js :
import {connect} from "react-redux";

import {countDecrement, countIncrement, countReset} from "../actions";
import ComponentCounter from "../components/Counter";

const mapStateToProps = state => ({
    count: state.count
});

const mapDispatchToProps = dispatch => ({

    onCountDecrement: (event, count) => {
        event.preventDefault();
        dispatch(countDecrement(count));
    },

    onCountIncrement: (event, count) => {
        event.preventDefault();
        dispatch(countIncrement(count));
    },

    onCountReset: event => {
        event.preventDefault();
        dispatch(countReset());
    }

});

export default connect(mapStateToProps, mapDispatchToProps)(ComponentCounter);

/src/components/Counter.js :
import React from "react";

const Counter = props => {

    const refInput = React.createRef();

    return (
        <>
            <h1>{props.count}</h1>
            <a href="#" onClick={event => props.onCountDecrement(event, parseInt(refInput.current.value))}>Минус</a>
            <a style={{"margin": "0 10px"}} href="#" onClick={event => props.onCountReset(event)}>Reset</a>
            <a href="#" onClick={event => props.onCountIncrement(event, parseInt(refInput.current.value))}>Плюс</a>
            <p>
                <input type="number" ref={refInput} defaultValue="1"/>
            </p>
        </>
    );
}

export default Counter;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-07-09
@Mike_Ro

Your path from the storage root to the value is no longer state.count, but state.count.count.
I recommend installing the Redux Dev Tools extension and adding it to the project.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question