I'm using react.js
, and I want to obtain from the state an array, but I required to clone it and save it into a const
. I can do it with two lines of code; exists a way to do it in a single line of code?
Example:
const { data } = this.state
const newData = [...data]
FYI, I'm using eslint, and with "parser": "babel-eslint"
config and VSC show me a warning if I'm trying to do this:
const data = [...this.state.data]
Warning message: Must use destructuring state assignments - lint react / destructuring-assignment
I don't want to remove/customize the rules of eslint... but I don't want to extend the code more than necessary.
Any suggestion?
Here's how you can create an Array slice of this.state.data
through destructuring all in one line:
const { data: [...newData] } = this.state
Caveat, this is not a deep clone if you're expecting that. It's just a new Array referencing the original Array items.
Note that I think your eslint rules are insane to require using destructuring. It's not always the best solution.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.