Coming from class-based object-oriented language C#, I can assign an object to a local variable so I can use it later. But in this example, when I assign it to a local variable, it will not render. When I console log the tempPersonObj
inside the handleClick, it will print the object. But it will print empty object inside the PersonDetails
function.
App.js
import React, {useState} from "react";
const persons = [
{
"name": "Arto Hellas",
"number": "040-123456",
"count": 3,
"id": 1
},
{
"name": "Ada Lovelace",
"number": "39-44-5323523",
"count": 10,
"id": 2
},
{
"name": "Mary Poppendieck",
"number": "39-23-6423122",
"count": 17,
"id": 3
}
]
const PersonDetails = ({person}) => {
console.log(person)
return (
<div>
<p>-----------------</p>
<p>{person.name}</p>
<p>{person.number}</p>
<p>{person.count}</p>
</div>
)
}
const App = () => {
const [view, setView] = useState(false)
const [personObj, setPersonObj] = useState({})
let tempPersonObj = {};
const handleClick = (person) => {
//This will work
// setPersonObj(person)
//This will not work
tempPersonObj = person
console.log(tempPersonObj)
setView(true)
}
return (
<div>
<h1>List:</h1>
{persons.map((person) => {
return (
<div key={person.id}>
{person.name} {""}
<button onClick={() => handleClick(person)}>View</button>
</div>
)})
}
{/* This will work */}
{/* {view && <PersonDetails person={tempPersonObj}/>} */}
{/* This will not work */}
{view && <PersonDetails person={personObj}/>}
</div>
);
}
export default App