当前位置: 首页 > 知识库问答 >
问题:

为'Route'提供的'object'类型的'component'属性无效,应为'function'`

邴英毅
2023-03-14

我已经为此挣扎了一段时间,似乎找不到其他有同样问题的人。我有这个组件和容器,它们是我的开始屏幕,应该在初始路径上呈现。

组件/困难选择。jsx

import React from 'react';
import PropTypes from 'prop-types';

export default class DifficultySelection extends React.Component{

  constructor(props){
    super(props);
    this.onClick = this.onClick.bind(this);
  }

  onClick(e){
    this.props.onDifficultyClick(e.target.innerHTML);

    this.props.history.push("/play");
  }

  render(){
    return (
      <div>
        <h2>Select your difficulty</h2>
        <div>
          <h3 onClick={this.onClick}>Easy</h3>
          <h3 onClick={this.onClick}>Medium</h3>
          <h3 onClick={this.onClick}>Hard</h3>
        </div>
      </div>
    )
  }
}

DifficultySelection.propTypes = {
  onDifficultyClick: PropTypes.func.isRequired
}

容器/困难选择。jsx

import DifficultySelection from '../components/DifficultySelection';
import { setBoardSize } from '../actions/actions';
import { connect } from 'react-redux';
import { withRouter } from 'react-router-dom';

const mapDispatchToProps = (dispatch, ownProps) => {
  return {
  onDifficultyClick: (difficulty) => {
      console.log(difficulty);
      let boardSize = 8;
      if(difficulty === "Medium")  boardSize = 12;
      if(difficulty === "Hard") boardSize = 15;
      if(difficulty === "Extreme") boardSize = 20;

      dispatch(setBoardSize(boardSize));
    }
  }
}

export default withRouter(connect(
  null,
  mapDispatchToProps
))(DifficultySelection)

下面是我尝试将这些组件分配到路由的地方。这就是我得到的错误。顺便说一句,我输入的控制台日志显示,我导入的组件是路由函数,其中包含的图像是因为我认为它应该是一个类。

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import registerServiceWorker from './registerServiceWorker';
import reducers from './reducers/reducers';
import { createStore, combineReducers } from 'redux';
import { Provider } from 'react-redux';
import { HashRouter, Route, Switch } from 'react-router-dom';

import DifficultySelection from './containers/DifficultySelection';
import Board from './containers/Board';

let store = createStore(combineReducers(reducers));

console.log(DifficultySelection.type);
console.log(Board.type);

ReactDOM.render(
  <Provider store={store}>
    <HashRouter>
      <Switch>
        <Route exact path="/" component={DifficultySelection}/>
        <Route exact path="/play" component={Board}/>
      </Switch>
    </HashRouter>
  </Provider>,
  document.getElementById('root'));
registerServiceWorker();

共有1个答案

墨阳羽
2023-03-14

问题在于:

export default withRouter(connect(
  null,
  mapDispatchToProps
))(DifficultySelection)

connect的语法是connect(MapStateTrops,mapDispatchToProps)(组件)

把你的电话放在一条线上,也许更容易看到错误:

withRouter(connect(null,mapDispatchToProps))(难以选择)

connect从不正确使用DifficultySelection作为一个片段,这意味着您的文件没有返回连接的组件。

只需按如下方式重新排列:

export default connect(null, mapDispatchToProps)(withRouter(DifficultySelection))

或者,如注释中所示,可以省略with Rout,因为此组件将从Road接收道具:

export default connect(null, mapDispatchToProps)(DifficultySelection)
 类似资料: