我有一个React.js组件,它在页面加载时呈现元素列表,但是可以使用过滤器更新此列表,该过滤器将从列表中删除某些元素并将修改后的列表保存在组件的状态中。我设法使这个功能正常工作,所以状态正确更新。
虽然元素可以正确地重新获得,但是当元素数量减少时,使用的空间更少。发生这种情况时,页面的高度不会缩小以适应页面占用的新大小,并在我的页脚下留下空内容,这是我页面的最后一个元素。
修改:
我对此更加了解,我认为React.js可能不是我问题的原因。在每次更新组件状态后,我调用一个使用imagesLoaded和Wookmark jQuery插件的函数,看起来在每次状态更改都不能正常工作后调用此函数。我应该在被调用的函数中改变一些东西,还是应该在另一个时间点调用该函数?
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);
});
}
答案 0 :(得分:0)
我发现Wookmark在listArticles部分中留下了隐藏的div,其中包含我的渲染元素,这解释了为什么我的页面底部有一个奇怪的空白区域。我尝试在每次渲染之前使用$(&#34; #listArticles .wookmark-placeholder&#34;)删除所有这些div。删除();它看起来很有效,但是组件的渲染速度有点慢。
为此,我将componentWillUpdate函数添加到了我的React组件:
componentWillUpdate: function() {
$("#listArticles .wookmark-placeholder").remove();
},