解构对象,在传递给组件时移除数组。

huangapple go评论56阅读模式
英文:

Destructure object, remove array when passing to component

问题

Sure, here's the translated part of your code:

"docs": [

],
"totalDocs": 18,
"offset": 0,
"limit": 10,
"totalPages": 2,
"page": 1,
"pagingCounter": 1,
"hasPrevPage": false,
"hasNextPage": true,
"prevPage": null,
"nextPage": 2
const queryFunction = async () => {
    setLoading(true)
    axios(configFetch)
        .then(res => {
            setData(res.data); // update state with response
        })
<Pager {...data} />

I've provided the translated code portions as requested.

英文:

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} />

答案1

得分: 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)
英文:

You can spread (...) out the docs, then capture 'the rest' and pass that to your &lt;Pager /&gt;:

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

return &lt;Pager {...otherProps} /&gt;

Runnable demo to show how it's working:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

const { docs, ...otherProps } = obj;
console.log(otherProps)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月17日 20:53:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定