Welcome back! Today, we'll be exploring advanced form validation, techniques for passing data from a child to a parent, strategies for handling events in parent components, and the concept of props.children
in React. Let's dive in!
To begin with, we'll be tackling form validation. It can be likened to a restaurant waiter confirming your order. In React, the useState
hook is beneficial for managing form values and validation rules.
JavaScript1import React, { useState } from "react"; 2 3function SimpleEmailForm() { 4 const [email, setEmail] = useState(""); 5 const handleEmailChange = (e) => { 6 setEmail(e.target.value); // The email state is updated as the user types 7 }; 8 // Validate if the entered email is in a valid format 9 const validateEmail = (e) => { 10 e.preventDefault(); 11 const pattern = // regular expression for email 12 pattern.test(email) ? alert('Email is valid') : alert('Invalid email'); 13 }; 14 return ( 15 <form onSubmit={validateEmail}> 16 <input type="text" value ={email} onChange={handleEmailChange} /> 17 <button type="submit">Submit</button> 18 </form> 19 ); 20}
Data in React typically flows from the top (parent) to the bottom (child). However, data can also flow in the upward direction using callback functions.
JavaScript1import React, { useState } from "react"; 2 3function Grandparent() { 4 const [grandChildData, setGrandChildData] = useState(""); 5 const handleCallback = (grandChildData) => { 6 setGrandChildData(grandChildData); // The state is updated with data from the grandchild 7 }; 8 return <Parent grandparentCallback={handleCallback}/>; 9} 10 11function Parent({ grandparentCallback }) { 12 return <Child parentCallback={grandparentCallback}/>; 13} 14 15function Child({ parentCallback }) { 16 return <button onClick={() => parentCallback("Data from Grandchild")}>Click</button>; 17}
In this case, the grandchild component sends data to the grandparent component.
Components in React trigger functions known as "event handlers" in response to user actions. Let's examine an example:
JavaScript1function Parent() { 2 const handleClick = () => console.log("Clicked in Child"); 3 return <Child onButtonClick={handleClick} />; 4} 5 6function Child({ onButtonClick }) { 7 return <button onClick={onButtonClick}>Click</button>; 8}
In this code, when the Child
component's button is clicked, the handleClick
function is triggered, which logs a message.
In React, props.children
allows developers to pass components as properties. This feature is particularly beneficial for creating 'wrapper' components.
JavaScript1function Box(props) { 2 return <div className='box'>{props.children}</div> // It renders the components passed to it 3} 4 5function App() { 6 return ( 7 <Box> 8 <h2>Hello there!</h2> // These components are passed to `Box` as `props.children` 9 <p>Welcome to React</p> 10 </Box> 11 ); 12}
This lesson has covered advanced form validation, the passing of data from child components to parent components, how to handle events in parent components, and the use of props.children
in React. Now comes the fun part: practice exercises to help solidify your understanding. Keep coding and mastering React!