1

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

1 回答 1

0

我发现 Wookmark 在包含我渲染元素的 listArticles 部分中留下了不可见的 div,这解释了为什么我的页面底部有一个奇怪的空白区域。我尝试在每次使用 $("#listArticles .wookmark-placeholder").remove(); 渲染之前删除所有这些 div 看起来它正在工作,但组件的渲染速度有点慢。

为此,我将 componentWillUpdate 函数添加到我的 React 组件中:

componentWillUpdate: function() {
    $("#listArticles .wookmark-placeholder").remove();
},
于 2015-02-23T13:35:32.363 回答