React.js组件中的状态更新在我的页面下留下了空白
Updating state in React.js component leaves blank space under my page
我有一个React.js组件,它在页面加载时呈现元素列表,但这个列表可以用一个过滤器更新,该过滤器将从列表中删除某些元素,并将修改后的列表保存在组件的状态中。我设法使这个功能发挥作用,所以状态得到了正确更新。
当元素被相应地重新排列时,当更少的元素被排列时,使用更少的空间。当这种情况发生时,页面的高度不会收缩以适应页面占用的新大小,并且在我的页脚下留下空内容,页脚是我页面的最后一个元素。
编辑:
我对此做了更多的研究,我认为React.js可能不是我问题的原因。每次更新组件状态后,我都会调用一个使用imagesLoaded和WookmarkjQuery插件的函数,而在每次状态更改后调用这个函数似乎效果不佳。被调用的函数中是否有应该更改的内容,或者我应该在另一个时间点调用该函数?
var ActivityListClass = React.createClass({
loggedInUser: {},
componentDidUpdate: function() {
activityListImageResponsiveness(); // This is the function
},
componentDidMount: function() {
var theActivityList = this;
var activitiesGetListener = postal.subscribe({
channel: "activities",
topic: "list",
callback: function(data, envelope) {
var dataClone = $.extend(true, {}, data);
dataClone.activities.shift();
theActivityList.setState({data: dataClone.activities});
theActivityList.loggedInUser = dataClone.loggedInUser;
}
});
},
getInitialState: function() {
return {data: []};
},
render: function() {
var i = 0;
var activityNodes = this.state.data.map(function(activity) {
var currentId = i;
i++;
return (
<ActivityClass key={currentId} data={activity} />
);
});
return (
<section id="listArticles">
{activityNodes}
</section>
);
}
});
调用的函数定义如下:
function activityListImageResponsiveness() {
$('#listArticles').imagesLoaded(function() {
// Get a reference to your grid items.
var $handler = $('#listArticles article');
$handler.on("webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend", function(event) {
if(event.originalEvent.propertyName == 'top'){
$('#listArticles').trigger("refreshWookmark");
}
});
// Prepare layout options.
var options = {
align: 'left',
autoResize: true, // This will auto-update the layout when the browser window is resized.
container: $('#listArticles'), // Optional, used for some extra CSS styling
direction: 'left',
fillEmptySpace: true, // Optional, fill the bottom of each column with widths of flexible height
flexibleWidth: '16.635%',
itemWidth: $handler.width(), // Optional, the width of a grid item
offset: 42, // Optional, the distance between grid items
verticalOffset: 15
};
var $window = $(window);
$window.resize(function() {
var windowWidth = $window.width(),
newOptions = {
flexibleWidth: '16.635%',
itemWidth: 208
};
// NOTE: These values are the same as the "media queries" of the CSS file.
if(windowWidth >= 1263){
newOptions.flexibleWidth = '16.635%';
newOptions.itemWidth = 208;
} else if (windowWidth >= 1132){
newOptions.flexibleWidth = '21.05263157894737%';
newOptions.itemWidth = 236;
}else if(windowWidth >= 1014){
newOptions.flexibleWidth = '20.71713147410359%';
newOptions.itemWidth = 208;
}else if(windowWidth >= 852){
newOptions.flexibleWidth = '27.99525504151839%';
newOptions.itemWidth = 236;
}else if(windowWidth >= 764){
newOptions.flexibleWidth = '27.51322751322751%';
newOptions.itemWidth = 208;
}else if(windowWidth >= 626){
newOptions.flexibleWidth = '42.58064516129032%';
newOptions.itemWidth = 264;
}else if(windowWidth >= 515){
newOptions.flexibleWidth = '40.7843137254902%';
newOptions.itemWidth = 208;
}else{
newOptions.flexibleWidth = '83.80952380952381%';
newOptions.itemWidth = 264;
}
$handler.wookmark(newOptions);
});
// Call the layout function.
$handler.wookmark(options);
});
}
我发现Wookmark在包含我渲染元素的listArticles部分留下了不可见的div,这解释了为什么我的页面底部有一个奇怪的空格。我试着在每次渲染之前用$("#listArticles.wookmark placeholder").remove()删除所有这些div;看起来它正在工作,但组件的渲染速度有点慢。
为此,我在React组件中添加了componentWillUpdate函数:
componentWillUpdate: function() {
$("#listArticles .wookmark-placeholder").remove();
},
相关文章:
- 为什么我的javascript地理编码函数返回空白结果
- 为什么我的网站在IE中是空白屏幕?(包括IE9!
- React.js组件中的状态更新在我的页面下留下了空白
- 为什么我的 JavaScript 提升的局部变量返回未定义,但提升的全局变量返回空白
- 我的网站在移动版式镶版式右侧显示空白区域,但桌面版式版式显示空白区域
- 为什么我的画布是空白的,除了在角落里
- 我怎么能强迫IE在它的左边保留一些空白'的浏览器窗口
- 我的移动网站右侧有一个令人讨厌的空白
- 为什么我的jquery.load()状态为空白
- 我的网站显示空白页面
- 这个Javascript代码广告使我的php页面空白,我该怎么办
- 我的jquery脚本是添加空白的html5视频
- 我的画布是空的,浏览器是空白的
- 为什么我的dropwon添加一个空白的空间angularjs
- 我的图像滑块有空白
- 我的html代码是不工作的目标空白
- 我如何添加两个问号和空白到我的RegEx
- Safari在我的图像下显示空白
- 外部JS文件导致我的网页显示为空白
- 为什么我的函数没有从空白输入框中获取值并正确执行函数