Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

search after click in the submit button with useInfiniteQuery

$
0
0

I'm trying react query and found useInfiniteQuery. then I start creating my infinite list of posts, but now I'd like to put an input to search for a specific post.

I have two endpoints: one for fetch all posts and the other to fetch by search.

how can I make that?

const [search, setSearch] = useState('');const {    data,    hasNextPage,    fetchNextPage  } = useInfiniteQuery(    ['posts', search],    ({ pageParam = 1 }) =>      !search        ? getPosts(companiesPerPage, pageParam)        : getPostsBySearch(search),    {      getNextPageParam: (lastPage, allPages) => {        const maxPages = Math.ceil(          lastPage.data.result.totalRecords / POSTS_PER_PAGE,        );        const nextPage = allPages.length + 1;        return nextPage <= maxPages ? nextPage : undefined;      },    },  );return (<input type="text" value={search} onChange={e => setSearch(e.target.value) /><ul>{posts?.pages.map(page =>                page.data.result.data.map((post, post) => (<li key={post.id}>{post.title}</li>                )),              )}</ul>)

Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>