- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在实现Tyler McGinnis curriculum学习 react 。
这是一个天气应用程序。而且我在调试奇怪的行为时遇到了麻烦。我很确定我正在做的事情很愚蠢,或者我可能错过了一些信息。
SearchContainer
是一个 ParentContainer,
var React = require("react");
var Search = require("../components/Search");
var SearchContainer = React.createClass({
propTypes: {
formType: React.PropTypes.string
},
contextTypes: {
router: React.PropTypes.object.isRequired
},
getDefaultProps: function() {
return {
formType: "form"
}
},
getInitialState: function() {
return {
city: ""
}
},
handleSearchSubmit: function(e) {
e.preventDefault();
this.context.router.push('/forecast/' + this.state.city);
},
handleCityChange: function(e) {
this.setState({
city: e.target.value
});
},
render() {
return (
<Search
formType={this.props.formType}
city={this.state.city}
onCityChange={this.handleCityChange}
onSearchSubmit={this.handleSearchSubmit}/>
);
}
})
module.exports = SearchContainer;
SearchContainer
更改上下文并切换到 ForecastContainer
,
var React = require("react");
var Forecast = require("../components/Forecast");
var Api = require("../helpers/Api");
var ForecastContainer = React.createClass({
getInitialState: function() {
return {
isLoading: true,
data: []
}
},
makeRequest: function(city) {
this.setState({
isLoading: true,
});
Api.getDayForecast(city).then( function(data) {
this.setState({
isLoading: false,
data: data.data.list
});
}.bind(this) );
},
componentDidMount: function() {
this.makeRequest(this.props.params.city);
},
componentWillReceiveProps: function(newProps) {
this.makeRequest(newProps.params.city);
},
render() {
return (
<Forecast isLoading={this.state.isLoading} data={this.state.data} />
)
}
});
module.exports = ForecastContainer;
现在,componentWillReceiveProps
被调用两次。我不明白为什么。从技术上讲,它应该只被调用一次。我正在 MakeRequest
方法中更新状态。状态改变后第二次调用。
我还附上了屏幕截图,以便更好地了解应用程序流程。
更新:
我使用的是 React-Router 版本 3.0.3。降级到 2.0.0 可以解决这个问题。更奇怪的是。
最佳答案
我无法告诉你为什么它被调用两次,但我可以告诉你这并不重要。问题是你没有比较 Prop 的变化。如果您这样做,代码将按照您想要的方式运行:
componentWillReceiveProps: function(newProps) {
if (newProps.params.city !== this.props.params.city) {
this.makeRequest(newProps.params.city);
}
},
另请参阅官方 ReactJS 文档,其中指出(强调我的): https://facebook.github.io/react/docs/react-component.html#componentwillreceiveprops
Note that React may call this method even if the props have not changed, so make sure to compare the current and next values if you only want to handle changes. This may occur when the parent component causes your component to re-render.
关于reactjs - 生命周期组件WillReceiveProps被多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43316125/
我正在开发一个使用多个 turtle 的滚动游戏。玩家 turtle 根据按键命令在 Y 轴上移动。当危害和好处在 X 轴上移动时,然后循环并改变 Y 轴位置。我尝试定义一个名为 colliding(
我不明白为什么他们不接受这个作为解决方案,他们说这是一个错误的答案:- #include int main(void) { int val=0; printf("Input:- \n
我正在使用基于表单的身份验证。 我有一个注销链接,如下所示: 以及对应的注销方法: public String logout() { FacesContext.getCurren
在 IIS7 应用程序池中有一个设置 Idle-time out 默认是 20 分钟,其中说: Amount of time(in minutes) a worker process will rem
我是一名优秀的程序员,十分优秀!