React Redux |无法更改减速器的状态

时间:2019-09-09 02:26:22

标签: javascript reactjs redux state reducers

我无法在减速器中更改状态“计数器”。

代码已达到要求,但计数器未定义 //减速器     从“ ../actiontypes/actiontypes”导入{ADDCOURSES};

const initialCourses = {
  counter: 0,
};

export const courses = (state = initialCourses, action) => {
  switch (action.type) {
    case ADDCOURSES:
      console.log('reached there');
      return {
        ...state,
        counter: 9,
      };

    default:
      return state;
  }
};

// ACTION

import { ADDCOURSES } from '../actiontypes/actiontypes';

export const addcourse = () => ({
  type: ADDCOURSES,
  payload: {
    counter: 2,
  },
});

// class

class Showcourses extends React.Component {
  render() {
    return (
      <div>
        <button onClick={hello}>+</button>
        <button onClick={this.props.addnewcourse}>-</button>
        {console.log(this.props)}
      </div>
    );
  }
}

function hello() {
  console.log('ksy ho');
}
const mapDispatchToProps = dispatch => ({
  addnewcourse: () => dispatch({ type: 'ADD_COURSES' }),
});

function mapStateToProps(state) {
  return {
    counter: state.counter,
  };
}

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

请以我为初学者的身份更正

0 个答案:

没有答案