×TypeError:Object(...)不是函数

时间:2019-11-26 07:54:18

标签: javascript reactjs redux react-redux

我正在尝试使用React和Redux构建一个简单的计数器,但是我被这个错误所困扰。错误出现在第23行,即const store=createStore(reducer)。我正在使用的React版本是16.8.6。

import React, { Component } from 'react';
import {render} from 'react-dom';
import {createStore} from 'react';
import {connect,Provider} from 'react-redux';
import './styles.scss';

const InitialState = {
  count: 0,
};

const IncrementValue = () => ({
  type: 'INCREMENT',
});

const reducer = (state = InitialState, action) => {
  if (action.type === 'INCREMENT') {
    return {
      count: state.count + 1,
    };
  }
  return state;
};
const store = createStore(reducer);

class Counter extends Component {
  render() {
    const { count, increment } = this.props;
    console.log({ count, increment });
    return (
      <main className="Counter">
        <p className="count">0</p>
        <section className="controls">
          <button>Increment</button>
          <button>Decrement</button>
          <button>Reset</button>
        </section>
      </main>
    );
  }
}
const mapStatetoProps = state => {
  return state;
};
const mapDispatchToProps = dispatch => {
  return {
    increment() {
      dispatch(IncrementValue());
    },
  };
};
const CounterContainer=connect(mapStatetoProps, mapDispatchToProps)(Counter);
render(
  <Provider store={store}>
    <CounterContainer/>
  </Provider>,
  document.getElementById('root'),
);

1 个答案:

答案 0 :(得分:0)

createStore是Redux的功能,而不是React的功能。 将导入更改为

import { createStore } from 'redux';