gpt4 book ai didi

reactjs - Redux:无法将函数映射到属性

转载 作者:行者123 更新时间:2023-12-03 14:15:13 25 4
gpt4 key购买 nike

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')
);

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

Warning: Failed propType: Invariant Violation: Home: prop type requestCall is invalid; it must be a function, usually from React.PropTypes. Check the render method of Connect(Home).

我错过了什么?

enter image description here enter image description here

最佳答案

问题不在于 Prop ,而在于 Prop 类型:

prop type requestCall is invalid; it must be a function, usually from React.PropTypes

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

改变

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

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

关于reactjs - Redux:无法将函数映射到属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34380429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com