`
import React, {Component} from 'react';
import {connect} from 'react-redux';
import Chart from '../components/chart.js';
import GoogleMap from '../containers/googlemap';
import {removeCityMap} from '../actions/index';
import { bindActionCreators } from "redux";
import AttributeSelector from './attributeselector'
class WeatherBoard extends Component{
constructor(props){
super(props);
this.state = {
chartData1: []
}
}
weatherAttrMapper(attrName){
//Setting the state which is based on the data received from action dispatched.
this.setState({
chartData1:this.props.weather[0].list.map(weather => weather.main[attrName])
})
}
renderMapList(cityData){
//Based on the weather prop received when action is dispached I want to set the state before rendering my <chart> element.
this.weatherAttrMapper('temp');
return(
<tr key = {cityName}>
<td><Chart data = {this.state.chartData1} color = "red" units="K"/></td>
</tr>
)
}
render(){
return(
<table className="table table-hover">
<tbody>
{this.props.weather.map(this.renderMapList.bind(this))}
</tbody>
</table>
)
}
}
//This is the weather state coming from my reducer which is being used here.
function mapStateToProps({ weather }) {
return { weather };
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({ removeCityMap }, dispatch);
}
export default connect(mapStateToProps,mapDispatchToProps)(WeatherBoard);
`我对状态管理有疑问。
问题陈述:我有一个容器 abc.JS 文件,它通过 mapStateToProps 映射到 redux 状态。我有一个按钮单击操作处理程序,它从 API 获取数据。当我的 actionhandler 被调度时,它会在 abc.js 中触发我的渲染方法。现在我的问题是我也在维护 abc.js 中的状态,该状态也用于 render 方法,并且需要在调度操作时进行修改。那么我如何设置我的 abc.js 状态,它也可以被渲染。
我还添加了我的 abc.js 的确切代码片段。所以基本上我在调度动作时输入我的渲染方法,我在这里我需要以某种方式设置状态。
谢谢。