【问题标题】:Redux: Couldn't map function to propertyRedux:无法将函数映射到属性
【发布时间】:2015-12-20 11:19:59
【问题描述】:
function mapStateToProps(state) {
  return {
    phoneNumber: state.phoneNumber,
  }
}

function mapDispatchToProps(dispatch) {
  return bindActionCreators({
      requestCall       // this is valid function
  }, dispatch)
}


class Home extends Component {
    constructor(props) {
        super(props);
        this.state = {
            phoneNumber: null
        };
    }
    render() {
        const { requestCall } = this.props;
        return (
            <div>
                <FlatButton label="Schedule" primary={true} onTouchTap={requestCall} value={this.state.phoneNumber}/>
            </div>
        )
    }
}

Home.propTypes = {
    requestCall: PropTypes.func.required
};

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

在不同的文件中,我需要像这样引用

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

const store = configureStore();

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

然后我在控制台中看到了这个

警告:失败的 propType:Invariant Violation:主页:prop 类型 requestCall 无效;它必须是一个函数,通常来自 React.PropTypes。检查Connect(Home)的渲染方法。

我错过了什么?

【问题讨论】:

  • 您有什么要求来电?您的 import requestCall 语句可能有问题吗?
  • @zhaozhiming 它只是返回一个带有类型的字典。
  • requestCall 定义在哪里?

标签: reactjs redux


【解决方案1】:

问题不在于道具,而在于道具类型:

道具类型 requestCall 无效;它必须是一个函数,通常来自React.PropTypes

问题出在.required 上,它不是正确的属性(因此您的道具类型值只是undefined)。

改变

Home.propTypes = {
    requestCall: PropTypes.func.required
};

到

Home.propTypes = {
    requestCall: PropTypes.func.isRequired
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-05
    • 2022-11-20
    相关资源
    最近更新 更多