在jsx内部调用forceUpdate
Calling forceUpdate inside jsx
我不擅长this
表示法,所以在jsx中调用方法forceUpdate()时遇到了一些问题。
class WeatherList extends Component {
renderWeather(cityData){
//some code
return(
<tr key={id}>
<td>
<div>{name}</div>
<h4 onClick={ () => {
var toChange = JSON.parse(localStorage.nameForData);
for(var i in toChange){
if(toChange[i].city.id = id){
toChange.splice(i,1);
}
}
localStorage.nameForData = JSON.stringify(toChange);
forceUpdate();
}} >Delete</h4>
</td>
<td><Chart data={temps} color='orange' units='K' /></td>
<td><Chart data={pressures} color='blue' units='hPa' /></td>
<td><Chart data={humidities} color='green' units='%' /></td>
</tr>
);
}
我试图在构造函数中将forceUpdate
绑定到this
,但我一次又一次地收到错误消息Cannot read property 'forceUpdate' of undefined
。
完整代码
class WeatherList extends Component {
renderWeather(cityData){
const name = cityData.city.name;
const temps = cityData.list.map(weather => weather.main.temp);
const pressures = cityData.list.map(weather=>weather.main.pressure);
const humidities = cityData.list.map(weather=>weather.main.humidity);
const id = cityData.city.id;
return(
<tr key={id}>
<td>
<div>{name}</div>
<h4 onClick={ () => {
var toChange = JSON.parse(localStorage.nameForData);
for(var i in toChange){
if(toChange[i].city.id = id){
toChange.splice(i,1);
}
}
localStorage.nameForData = JSON.stringify(toChange);
forceUpdate();
}} >Delete</h4>
</td>
<td><Chart data={temps} color='orange' units='K' /></td>
<td><Chart data={pressures} color='blue' units='hPa' /></td>
<td><Chart data={humidities} color='green' units='%' /></td>
</tr>
);
}
render(){
var arr = JSON.parse(localStorage.getItem('nameForData'));
arr = arr.concat(this.props.weather);
localStorage.setItem('nameForData', JSON.stringify(arr));
//localStorage.setItem('nameForData', JSON.stringify(this.props.weather));
console.log(arr);
return(
<table className="table">
<thead>
<tr>
<th>City</th>
<th>Temperature (K)</th>
<th>Pressure (hPa)</th>
<th>Humidity (%)</th>
</tr>
</thead>
<tbody>
{arr.map(this.renderWeather)}
</tbody>
</table>
);
}
}
您不应该绑定forceUpdate
函数。通过调用this.forceUpdate()
来使用它。
正确代码:
// `renderWeather` function:
<h4 onClick={ () => {
var toChange = JSON.parse(localStorage.nameForData);
for(var i in toChange){
if(toChange[i].city.id = id){
toChange.splice(i,1);
}
}
localStorage.nameForData = JSON.stringify(toChange);
this.forceUpdate();
}} >Delete</h4>
当您将this.renderWeather
函数传递给map
时,它的this
上下文将是undefined
,要设置正确的this
上下文,您应该将其作为第二个参数传递给map
函数:
// `render` function:
<tbody>
{arr.map(this.renderWeather, this)}
</tbody>
相关文章:
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- D3在一个调用中绘制不同的SVG形状,没有可见性
- 如何从Java/scala调用js美化程序
- 如何调用这个匿名 JavaScript 函数
- 如何从模块链中调用函数.导出到节点中
- 我需要从php调用javascript或jquery
- Chrome开发工具(如何知道我在调用哪个javascript对象)
- 单击按钮后如何逐个调用分区,上一个分区将隐藏
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- Twitter Bootstrap typeahead:使用“this”获取上下文/调用元素
- node.js:setInterval()正在跳过调用
- 如何在单击复选框后调用控制器方法
- 如何在JQuery函数中定义一个值,然后调用另一个函数并使用该值
- 使用Google Visualization动态调用构造函数
- 如何在webView,Android中从@JavascriptInterface方法调用Javascript
- 是否可以将一个函数输入连接到另一个函数调用的文本
- 调用函数内部的函数
- 函数未在Object.keys或Object.getOwnPropertyNames下列出,但可以调用
- 当 redux 状态更新时,在组件上调用 forceUpdate()
- 在jsx内部调用forceUpdate