英文:
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 <Pager />
:
const { docs, ...otherProps } = data;
return <Pager {...otherProps} />
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: 'foo',
bar: 'bar'
};
const { docs, ...otherProps } = obj;
console.log(otherProps)
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论