this.props.myActionCreator throws 'TypeError: Cannot read property 'props' of null'
this.props.myActionCreator throws 'TypeError: Cannot read property 'props' of null'
顺便提一下,我没有在类似 post 上找到适合我的特定情况的答案。
我的应用程序允许用户获取城市的天气以及该天气的相关装箱单。
我想让每个城市的卡片都可删除,并放置一个 'X' 应该调用 updateTerms 动作创建者,后者会依次将其从用户列表中删除并更新 dom。
回调似乎触发了,但是 this.props.updateTerms 抛出 **TypeError: Cannot read 属性 'props' of null
在 handleDeleteCard **
a_result_card.js
import React, { Component } from 'react';
import { connect } from 'react-redux';
import ATodoList from './a_Todo_List';
import CSSTransitionGroup from 'react-transition-group/CSSTransitionGroup';
import { bindActionCreators} from 'redux';
import { updateTerms } from '../actions';
class AResultCard extends Component {
constructor(props) {
super(props);
this.state = { cityName : '' };
this.handleDeleteCard = this.handleDeleteCard.bind(this);
}
handleDeleteCard(event) { // I NEED TO CALL THIS WITH THE KEY
//So THAT I CAN REMOVE IT FROM THE USERS TERM LIST
event.preventDefault();
debugger;
this.props.updateTerms(cityName, "delete");
}
renderWeather(cityData) {
function calcAverageTemp(days) {
let reduced = days.reduce((sum,day) => {
return sum + day.main.temp_max;
}, 0);
return Math.floor(reduced / cityData.list.length);
};
function calcAverageHumidity(days) {
let reduced = days.reduce((sum,day) => {
return sum + day.main.humidity;
}, 0);
return Math.floor(reduced / cityData.list.length);
};
const avgTempK = calcAverageTemp(cityData.list);
const cityName = cityData.city.name;
const avgTempF = Math.floor((avgTempK * 1.8) - 459.67 );
const avgHumidity = calcAverageHumidity(cityData.list);
return (
<li key={cityName} className="result-card">
<div className="card">
<div className="">
<div className="weather-details">
<div className="card-heading">
<span className="weather-detail detail-title">{cityName}</span>
<span className="weather-detail">{avgTempF} °F</span>
<span className="weather-detail">H: {avgHumidity}%</span>
<span className="weather-detail delete-card-icon" onClick={handleDeleteCard}>X</span>
</div>
</div>
<ATodoList avgTempF={avgTempF} />
</div>
</div>
</li>
);
}
render() {
return (
<ul className="results">
<CSSTransitionGroup
transitionName="example"
transitionEnterTimeout={150}
transitionLeaveTimeout={150}>
{this.props.weather.map(this.renderWeather)}
</CSSTransitionGroup>
</ul>
);
}
}
function mapStateToProps({weather, userTerms}) {
return ({
weather,
userTerms
});
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({ updateTerms }, dispatch);
}
export default connect(mapStateToProps, mapDispatchToProps)(AResultCard);
您需要像
这样调用 handleDeleteCard
函数
<span className="weather-detail delete-card-icon" onClick={this.handleDeleteCard}>X</span>
因为它属于 React Component
而不是 renderWeather
函数的上下文
同时绑定 renderWeather 函数
renderWeather = (cityData) => {
function calcAverageTemp(days) {
let reduced = days.reduce((sum,day) => {
return sum + day.main.temp_max;
}, 0);
return Math.floor(reduced / cityData.list.length);
};
function calcAverageHumidity(days) {
let reduced = days.reduce((sum,day) => {
return sum + day.main.humidity;
}, 0);
return Math.floor(reduced / cityData.list.length);
};
const avgTempK = calcAverageTemp(cityData.list);
const cityName = cityData.city.name;
const avgTempF = Math.floor((avgTempK * 1.8) - 459.67 );
const avgHumidity = calcAverageHumidity(cityData.list);
return (
<li key={cityName} className="result-card">
<div className="card">
<div className="">
<div className="weather-details">
<div className="card-heading">
<span className="weather-detail detail-title">{cityName}</span>
<span className="weather-detail">{avgTempF} °F</span>
<span className="weather-detail">H: {avgHumidity}%</span>
<span className="weather-detail delete-card-icon" onClick={this.handleDeleteCard}>X</span>
</div>
</div>
<ATodoList avgTempF={avgTempF} />
</div>
</div>
</li>
);
}
顺便提一下,我没有在类似 post 上找到适合我的特定情况的答案。
我的应用程序允许用户获取城市的天气以及该天气的相关装箱单。
我想让每个城市的卡片都可删除,并放置一个 'X' 应该调用 updateTerms 动作创建者,后者会依次将其从用户列表中删除并更新 dom。
回调似乎触发了,但是 this.props.updateTerms 抛出 **TypeError: Cannot read 属性 'props' of null 在 handleDeleteCard **
a_result_card.js
import React, { Component } from 'react';
import { connect } from 'react-redux';
import ATodoList from './a_Todo_List';
import CSSTransitionGroup from 'react-transition-group/CSSTransitionGroup';
import { bindActionCreators} from 'redux';
import { updateTerms } from '../actions';
class AResultCard extends Component {
constructor(props) {
super(props);
this.state = { cityName : '' };
this.handleDeleteCard = this.handleDeleteCard.bind(this);
}
handleDeleteCard(event) { // I NEED TO CALL THIS WITH THE KEY
//So THAT I CAN REMOVE IT FROM THE USERS TERM LIST
event.preventDefault();
debugger;
this.props.updateTerms(cityName, "delete");
}
renderWeather(cityData) {
function calcAverageTemp(days) {
let reduced = days.reduce((sum,day) => {
return sum + day.main.temp_max;
}, 0);
return Math.floor(reduced / cityData.list.length);
};
function calcAverageHumidity(days) {
let reduced = days.reduce((sum,day) => {
return sum + day.main.humidity;
}, 0);
return Math.floor(reduced / cityData.list.length);
};
const avgTempK = calcAverageTemp(cityData.list);
const cityName = cityData.city.name;
const avgTempF = Math.floor((avgTempK * 1.8) - 459.67 );
const avgHumidity = calcAverageHumidity(cityData.list);
return (
<li key={cityName} className="result-card">
<div className="card">
<div className="">
<div className="weather-details">
<div className="card-heading">
<span className="weather-detail detail-title">{cityName}</span>
<span className="weather-detail">{avgTempF} °F</span>
<span className="weather-detail">H: {avgHumidity}%</span>
<span className="weather-detail delete-card-icon" onClick={handleDeleteCard}>X</span>
</div>
</div>
<ATodoList avgTempF={avgTempF} />
</div>
</div>
</li>
);
}
render() {
return (
<ul className="results">
<CSSTransitionGroup
transitionName="example"
transitionEnterTimeout={150}
transitionLeaveTimeout={150}>
{this.props.weather.map(this.renderWeather)}
</CSSTransitionGroup>
</ul>
);
}
}
function mapStateToProps({weather, userTerms}) {
return ({
weather,
userTerms
});
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({ updateTerms }, dispatch);
}
export default connect(mapStateToProps, mapDispatchToProps)(AResultCard);
您需要像
这样调用handleDeleteCard
函数
<span className="weather-detail delete-card-icon" onClick={this.handleDeleteCard}>X</span>
因为它属于 React Component
而不是 renderWeather
函数的上下文
同时绑定 renderWeather 函数
renderWeather = (cityData) => {
function calcAverageTemp(days) {
let reduced = days.reduce((sum,day) => {
return sum + day.main.temp_max;
}, 0);
return Math.floor(reduced / cityData.list.length);
};
function calcAverageHumidity(days) {
let reduced = days.reduce((sum,day) => {
return sum + day.main.humidity;
}, 0);
return Math.floor(reduced / cityData.list.length);
};
const avgTempK = calcAverageTemp(cityData.list);
const cityName = cityData.city.name;
const avgTempF = Math.floor((avgTempK * 1.8) - 459.67 );
const avgHumidity = calcAverageHumidity(cityData.list);
return (
<li key={cityName} className="result-card">
<div className="card">
<div className="">
<div className="weather-details">
<div className="card-heading">
<span className="weather-detail detail-title">{cityName}</span>
<span className="weather-detail">{avgTempF} °F</span>
<span className="weather-detail">H: {avgHumidity}%</span>
<span className="weather-detail delete-card-icon" onClick={this.handleDeleteCard}>X</span>
</div>
</div>
<ATodoList avgTempF={avgTempF} />
</div>
</div>
</li>
);
}