Created
February 13, 2025 07:17
-
-
Save balvinder294/9beb222ecc5006b755343065f0ba5941 to your computer and use it in GitHub Desktop.
Parent Child Component Example as in Article https://tekraze.com/understanding-states-and-props-in-reactjs-component/
This file contains hidden or 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'; | |
function ChildComponent(props) { | |
return ( | |
<p>Message from parent: {props.message}</p> | |
); | |
} | |
function ParentComponent() { | |
const [parentMessage, setParentMessage] = useState("Initial message"); | |
const updateMessage = () => { | |
setParentMessage("New message from parent!"); | |
}; | |
return ( | |
<div> | |
<button onClick={updateMessage}>Update Message</button> | |
<ChildComponent message={parentMessage} /> | |
</div> | |
); | |
} | |
export default ParentComponent; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment