I'm using React hooks and trying to set the initial state of a constant inside a component with the passed down props
. I've seen multiple examples of this online, but when I do it, the array destructuring returns the whole props
object, not the instance I want.
Import React, {useState} from "react";
const MyComponent = (props) => {
console.log(props) // {date: "2019-11-26", task: "cooking"}
const [date, setDate] = useState(props);
console.log(date) // {date: "2019-11-26, task: "cooking"}
return (...)
}
export default MyComponent;
I'd assume that with object/array destructuring the value of date
would automatically be assigned the value from props
(the string "2019-11-26"
), not the whole props
object. What am I missing? I can get around this by setting const [date, setDate] = useState(props.date)
but that breaks eslints react plugins destructuring rule and I'd like to avoid it.
Edit
Thanks to helloitsjoe for an answer that solves my problem!