我在react-redux中实现了一个天气小工具,我收到了错误" TypeError:_this2.props.handleCityChange不是一个函数"和所有功能的类似错误。我已经浏览了redux文档,大量谷歌搜索和类似的stackoverflow问题,我仍然无法理解问题所在,此时我愿意尝试几乎任何事情。
我删除了大部分代码以减少问题的代码片段大小,只保留一小部分来解释问题,如果您需要完整代码并让我更新,请告诉我。< / p>
这是容器,
import { isChangingCity} from '../actions/weather.act'
import { connect } from 'react-redux'
const mapStateToProps = (state) => ({
status: state.weather.weather.status,
tempFormat: state.weather.format,
temp: state.weather.weather.temp,
isFetching: state.weather.isFetching,
isChangingCity: state.weather.isChangingCity
})
const mapDispatchToProps = (dispatch) => ({
handleCityChange: () => dispatch(isChangingCity(true)),
});
const weather = connect(mapStateToProps, mapDispatchToProps)(Weather)
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
&#13;
和组件,
import React from 'react'
class Weather extends React.PureComponent{
render(){
return(
<span>
<button className = "btn btn-light" onClick={() => this.props.handleCityChange()} >
Change City
</button>
</span>
);
}
}
export default Weather
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
&#13;
感谢所有帮助。
答案 0 :(得分:0)
import React from 'react'
import container from "pathToContainer"
class Weather extends React.PureComponent{
render(){
return(
<span>
<button className = "btn btn-light" onClick={() => this.props.handleCityChange()} >
Change City
</button>
</span>
);
}
}
export default container(Weather)
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
&#13;