Last active
June 23, 2021 06:19
-
-
Save Frodigo/882751d9f86fd08435cd9c068c6935bf to your computer and use it in GitHub Desktop.
React prop drilling example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from "react"; | |
export const Account = () => { | |
const [user, setUser] = useState({ name: "Marcin", country: "Poland" }); | |
<AccountDetails user={user} setUser={setUser} />; | |
}; | |
export const EditAccount = (props) => { | |
const { user, setUser } = props; | |
return ( | |
<form> | |
<input | |
type="text" | |
value={user.name} | |
onChange={(e) => | |
setUser({ | |
...user, | |
name: e.target.value, | |
}) | |
} | |
/> | |
<input | |
type="text" | |
value={user.country} | |
onChange={(e) => | |
setUser({ | |
...user, | |
country: e.target.value, | |
}) | |
} | |
/> | |
</form> | |
); | |
}; | |
export const AccountDetails = props => { | |
const { user, setUser } = props; | |
return ( | |
<> | |
<h1>{user.name}</h1> | |
<p>Country: {user.country}</p> | |
<EditAccount user={user} setUser={setUser}></EditAccount> | |
</> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment