React.js组件中的状态更新在我的页面下留下了空白

Updating state in React.js component leaves blank space under my page

本文关键字:空白 我的 组件 js 更新 状态 React      更新时间:2024-02-19

我有一个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();
},