繁体   English   中英

如何制作 Api 的过滤列表并在反应中实现无限滚动的分页

[英]how to make filtered list of Api and implement pagination of infinite scrolling in react

这仅适用于过滤第 1 页,但在使用 react-infinite-scroll-component 实现无限滚动后,它不会 append 第 2 页通过使用 spead 运算符得到第 1 页的结果,它使 setcontacts 成为一个数组数组,任何人都可以帮助我这个

const searchState =  (async fetchData => {
    const requests= await axios({
        method: 'GET',
        url: 'api url',
        params: { name: Search, page: page },
    });
    const cons= requests.data.results;
    console.log(cons);
    // console.log(cons.location222.name)
    let  matches = cons.filter(con => {
        const regex = new RegExp(`^${fetchData}`,'gi');

        return con.name.match(regex);
    });

    if (fetchData.length === 0) {
        matches=[];
    }

    // setLoading(true)
    setContacts(matches);

    // setContacts(prevcontacts=>{ return [...prevcontacts,matches]})
});

使用 React 组件无限加载内容。 我正在使用"React Infinite Scroller" 这个 fork 维护了一个简单、轻量级的无限滚动 package,它支持 window 和可滚动元素。

Window 滚动事件

<InfiniteScroll
    pageStart={0}
    loadMore={loadFunc}
    hasMore={true || false}
    loader={<div className="loader" key={0}>Loading ...</div>}
>
    {items} // <-- This is the content you want to load
</InfiniteScroll>

DOM 滚动事件

<div style="height:700px;overflow:auto;">
    <InfiniteScroll
        pageStart={0}
        loadMore={loadFunc}
        hasMore={true || false}
        loader={<div className="loader" key={0}>Loading ...</div>}
        useWindow={false}
    >
        {items}
    </InfiniteScroll>
</div>

自定义父元素 您可以定义自定义 parentNode 元素以作为滚动计算的基础。

<div style="height:700px;overflow:auto;" ref={(ref) => this.scrollParentRef = ref}>
    <div>
        <InfiniteScroll
            pageStart={0}
            loadMore={loadFunc}
            hasMore={true || false}
            loader={<div className="loader" key={0}>Loading ...</div>}
            useWindow={false}
            getScrollParent={() => this.scrollParentRef}
        >
            {items}
        </InfiniteScroll>
    </div>
</div>

如何进行无限滚动<div> ?</div><div id="text_translate"><p> 我试图让这个带有列的 div 无限向下移动 -<a href="https://i.stack.imgur.com/Gjswj.png" rel="nofollow noreferrer">示例</a>,就像在这个谷歌开发者页面上 - <a href="https://developers.google.com/web/updates/2016/07/infinite-scroller" rel="nofollow noreferrer">谷歌开发者</a>|| <a href="https://i.stack.imgur.com/MssUX.png" rel="nofollow noreferrer">无限滚动图像</a>,但如您所见,不成功。 因此,我将&lt;div&gt;称为“imgs”,其中包含一些图像,并且当某些 position 中的“imgs”时,来自 div 底部的图像被移动到顶部,但用户不应该看到它们移动。 以下是我的一些作品: </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> let imgsContainer = document.getElementById("imgs"); let imgAppear = document.getElementById("imgAppear"); function imgsContainerMove() { let top = 0; setInterval(imgsContainerMoving, 25); function imgsContainerMoving() { // Animation top++; imgsContainer.style.top = top + '%'; if (top === 100) top = 0; //Top reset if (imgsContainer.style.top == "1%") { Array.from(document.querySelectorAll("div.imgs &gt; img")).slice(-6).forEach((image) =&gt; imgAppear.prepend(image)) //Adding images to the top } } } imgsContainerMove(); // execute function</pre><pre class="snippet-code-css lang-css prettyprint-override"> body{ margin: 0; }.gallery-block { display: flex; flex-flow: column; justify-content: center; align-items: center; width: 100%; background-color: #2B3C35; }.gallery-bg { position: relative; margin: 50px 0; padding: 0 10px; width: 100%; height: 40vh; overflow: hidden; display: flex; justify-content: center; }.gallery-bg.overlay { position: absolute; top: 0; bottom: 0; right: 0; left: 0; width: 100%; height: 100%; z-index: auto; background: rgb(43, 60, 53); background: -moz-linear-gradient(0deg, rgba(43, 60, 53, 1) 0%, rgba(1, 2, 2, 0) 50%, rgba(43, 60, 53, 1) 100%); background: -webkit-linear-gradient(0deg, rgba(43, 60, 53, 1) 0%, rgba(1, 2, 2, 0) 50%, rgba(43, 60, 53, 1) 100%); background: linear-gradient(0deg, rgba(43, 60, 53, 1) 0%, rgba(1, 2, 2, 0) 50%, rgba(43, 60, 53, 1) 100%); }.imgs { position: absolute; width: 75vw; columns: 200px; column-gap: 15px; }.imgs img { width: 100%; margin-bottom: 15px; border-radius: 5px; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;div class="gallery-block"&gt; &lt;h2&gt;Gallery&lt;/h2&gt; &lt;img src="img/triangle.png" alt="" class="triang"&gt; &lt;div class="gallery-bg"&gt; &lt;div class="imgs" id="imgs"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;/div&gt; &lt;div class="overlay"&gt;&lt;/div&gt; &lt;/div&gt;</pre></div></div><p></p></div>

[英]How to make infinite scrolling <div>?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 React 中实现无限数据的无限滚动? 如何进行无限滚动? 如何在Rails中实现无限滚动 Rails中的分页和无限滚动 如何进行无限滚动<div> ?</div><div id="text_translate"><p> 我试图让这个带有列的 div 无限向下移动 -<a href="https://i.stack.imgur.com/Gjswj.png" rel="nofollow noreferrer">示例</a>,就像在这个谷歌开发者页面上 - <a href="https://developers.google.com/web/updates/2016/07/infinite-scroller" rel="nofollow noreferrer">谷歌开发者</a>|| <a href="https://i.stack.imgur.com/MssUX.png" rel="nofollow noreferrer">无限滚动图像</a>,但如您所见,不成功。 因此,我将&lt;div&gt;称为“imgs”,其中包含一些图像,并且当某些 position 中的“imgs”时,来自 div 底部的图像被移动到顶部,但用户不应该看到它们移动。 以下是我的一些作品: </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> let imgsContainer = document.getElementById("imgs"); let imgAppear = document.getElementById("imgAppear"); function imgsContainerMove() { let top = 0; setInterval(imgsContainerMoving, 25); function imgsContainerMoving() { // Animation top++; imgsContainer.style.top = top + '%'; if (top === 100) top = 0; //Top reset if (imgsContainer.style.top == "1%") { Array.from(document.querySelectorAll("div.imgs &gt; img")).slice(-6).forEach((image) =&gt; imgAppear.prepend(image)) //Adding images to the top } } } imgsContainerMove(); // execute function</pre><pre class="snippet-code-css lang-css prettyprint-override"> body{ margin: 0; }.gallery-block { display: flex; flex-flow: column; justify-content: center; align-items: center; width: 100%; background-color: #2B3C35; }.gallery-bg { position: relative; margin: 50px 0; padding: 0 10px; width: 100%; height: 40vh; overflow: hidden; display: flex; justify-content: center; }.gallery-bg.overlay { position: absolute; top: 0; bottom: 0; right: 0; left: 0; width: 100%; height: 100%; z-index: auto; background: rgb(43, 60, 53); background: -moz-linear-gradient(0deg, rgba(43, 60, 53, 1) 0%, rgba(1, 2, 2, 0) 50%, rgba(43, 60, 53, 1) 100%); background: -webkit-linear-gradient(0deg, rgba(43, 60, 53, 1) 0%, rgba(1, 2, 2, 0) 50%, rgba(43, 60, 53, 1) 100%); background: linear-gradient(0deg, rgba(43, 60, 53, 1) 0%, rgba(1, 2, 2, 0) 50%, rgba(43, 60, 53, 1) 100%); }.imgs { position: absolute; width: 75vw; columns: 200px; column-gap: 15px; }.imgs img { width: 100%; margin-bottom: 15px; border-radius: 5px; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;div class="gallery-block"&gt; &lt;h2&gt;Gallery&lt;/h2&gt; &lt;img src="img/triangle.png" alt="" class="triang"&gt; &lt;div class="gallery-bg"&gt; &lt;div class="imgs" id="imgs"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;img src="https://i.picsum.photos/id/244/300/200.jpg?hmac=EveIsD4iO8woArdqcsJM76yYG6rwDR7OmPuPRwxh_1w"&gt; &lt;/div&gt; &lt;div class="overlay"&gt;&lt;/div&gt; &lt;/div&gt;</pre></div></div><p></p></div> 如何在AngularJs中的无限滚动中实现后退按钮? 如何使用新的Firebase(2016)实现无限滚动? react.js中如何实现对表中列表数据的分页? 如何使用Github API存储库列表实现无限滚动 如何使用 react-virualized 在表格上实现无限滚动?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM