0

I would like to pass the paging data to my component without passing the docs array.

"docs": [
   
],
"totalDocs": 18,
"offset": 0,
"limit": 10,
"totalPages": 2,
"page": 1,
"pagingCounter": 1,
"hasPrevPage": false,
"hasNextPage": true,
"prevPage": null,
"nextPage": 2

I am setting the state from my axios get:

const queryFunction = async () => {
    setLoading(true)
    axios(configFetch)
        .then(res => {
            setData(res.data); // update state with response
        })

However, I am passing all of the data to my component, how can I just pass the paging data without the docs array?

<Pager {...data} />
0stone0
  • 34,288
  • 4
  • 39
  • 64
Bomber
  • 10,195
  • 24
  • 90
  • 167
  • 1
    I think we can't exclude a specific. How about `{...data, docs: []}` ? Explicitly initialize it with empty array or even `null` – Shri Hari L Apr 17 '23 at 12:57

1 Answers1

4

You can spread (...) out the docs, then capture 'the rest' and pass that to your <Pager />:

const { docs, ...otherProps } = data;

return <Pager {...otherProps} />

Runnable demo to show how it's working:

const obj = {
    docs: [ 1, 2, 3 ],
    foo: 'foo',
    bar: 'bar'
};

const { docs, ...otherProps } = obj;
console.log(otherProps)
0stone0
  • 34,288
  • 4
  • 39
  • 64