【问题标题】:Invalid mapStateToProps Argument in React-ReduxReact-Redux 中的 mapStateToProps 参数无效
【发布时间】:2017-12-06 18:46:52
【问题描述】:

我遇到了 ma​​pStateToProps 参数的问题。这似乎是一个非常简单的错误,但我无法弄清楚发生了什么。基本上,我想做的是用 react-redux 和 react saga 切换侧边栏菜单。一切正常,但我收到以下错误:

未捕获的错误:连接组件侧边栏时,mapStateToProps 参数的类型对象的值无效。

感谢任何帮助:

索引 app/js/index.js

import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import configureStore from '../store/configureStore';
import Canvas from '../components/Canvas/Canvas';

const store = configureStore();

render(
  <Provider store={store}>
    <Canvas />
  </Provider>,
  document.getElementById('app'),
);

减速器

(索引)app/reducers/index.js

import { combineReducers } from 'redux';
import sidebar from './sidebar';

const rootReducer = combineReducers({
  sidebar,
});

export default rootReducer;

(侧边栏)app/reducers/sidebar.js

import {
  TOGGLE_SIDEBAR,
} from '../actions';

const sidebar = (state = { value: true }, action) => {
  switch (action.type) {
    case TOGGLE_SIDEBAR:
      debugger;
      return action.value;
    default:
      debugger;
      return state.value;
  }
}

export default sidebar;

传奇

index.js app/sagas/index.js

import { fork } from 'redux-saga/effects';
import { watcher } from './watcher';

function* rootSaga() {
  yield [
    fork(watcher),
  ];
}

export default rootSaga;

sidebar.js app/sagas/sidebar.js

import { put } from 'redux-saga/effects';
import {
  TOGGLE_SIDEBAR,
} from '../actions';

export function* sidebar () {
  try {
    yield put ({ type: TOGGLE_SIDEBAR });
  } catch (err) {
    debugger;
  }
}

watcher.js app/sagas/watcher.js

import { takeEvery } from 'redux-saga/effects';
import { sidebar } from './sidebar';
import {
  TOGGLE_SIDEBAR,
} from '../actions';

export function* watcher() {
  try {
    yield takeEvery(TOGGLE_SIDEBAR, sidebar);
  } catch (err) { debugger; }
}

操作 app/actions/index.js

export const TOGGLE_SIDEBAR = 'TOGGLE_SIDEBAR';

export const toggleSidebar = (value) => ({
  type: TOGGLE_SIDEBAR,
  value,
});

容器 SidebarContainer.js app/containers/sidebarContainer.js

import { connect } from 'react-redux';
import {
  toggleSidebar as callToggleSidebar,
} from '../actions';
import Sidebar from '../components/Sidebar/Sidebar';

debugger;
const mapStateToProps = (state) => {
  debugger;
  return {
    open: state.sidebar,
  }
};

const mapDispatchToProps = dispatch => ({
  toggleSidebar: (val) => { dispatch(callToggleSidebar(val)); },
});

export default connect({
  mapStateToProps,
  mapDispatchToProps,
})(Sidebar);

组件

Sidebar.jsx app/components/Sidebar.jsx

import React from 'react';
import { PropTypes } from 'prop-types';
import './styles/styles.less';

const Sidebar = ({ open, toggleSidebar }) => (
  <div className={open ? 'sidebar sidebar-open' : 'sidebar sidebar-closed'}>
    <div className='show-hide-container'>
      <button onClick={() => toggleSidebar(!open)}>
        <i className="fa fa-arrow-right" aria-hidden="true" />
      </button>
    </div>
    Sidebar
  </div>
);

Sidebar.propTypes = {
  open: PropTypes.bool.isRequired,
  toggleSidebar: PropTypes.func.isRequired,
};

export default Sidebar;

【问题讨论】:

  • 嘿@AndrewLi,我试过了,但没用。我相信不会,因为我的减速器已经返回一个布尔值。谢谢。

标签: reactjs react-redux redux-saga


【解决方案1】:
export default connect(
  mapStateToProps,
  mapDispatchToProps,
)(Sidebar);

请尝试,将mapStateToPropsmapDispatchToProps 作为参数而不是对象传递。

connect 是一个期望状态和调度函数的函数。

https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options

【讨论】:

  • 嘿伙计,真可惜,呵呵,很简单的基本错误。非常感谢您的帮助!
  • 酷。谢谢:)
  • b 的儿子...我花了一个小时才找到这个。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-16
  • 1970-01-01
  • 1970-01-01
  • 2019-08-01
  • 2017-05-17
  • 2016-11-07
相关资源
最近更新 更多