const [val, setVal]=useState("some");
const change=()=>{
setVal(5);
add();
}
const add=()=>{
console.log(val);
}
export default function App() {
const [emp, setEmp] = useState({ name: "A", age: 5 });
const update = () => {
emp.age = 10;
console.log()
setEmp(emp);
};
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>{`${emp.name} : ${emp.age}`}</h2>
<button onClick={update}>Update</button>
</div>
);
}
import { useState } from "react";
const App = () => {
const [user, setUser] = useState({ firstName: "", lastName: "" });
const updateUser = (first, last) => {
setUser({ ...user, firstName: first });
if (last) {
updateLastName(last);
}
};
const updateLastName = (last) => {
setUser((us)=>{ ...user, lastName: last });
};
return (
<>
<div>
First Name: {user.firstName}
<br />
Last Name: {user.lastName}
</div>
<button onClick={() => updateUser("Tom", "Cruise")}>Update User</button>
</>
);
};
export default App;
redux -> multiple slices update, combine reducer
where to use redux and where to use contextAPI
when to use keys in react
Comments
Post a Comment