Today in this blog, we will aware about Props means (short for properties) are used to pass data from a parent component to a child component by using reactjs. So, without wasting any time lets do it but lets recap what is exactly React js.
how to pass data parent to child component using reactjs
React
Certainly! ReactJS is a JavaScript library developed by
Facebook for building user interfaces. It allows developers to create
reusable UI components and efficiently manage the state of their
applications. Here are some key aspects of ReactJS:
Component-Based Architecture: ReactJS follows a component-based
architecture, where the user interface is divided into small, reusable
components. Components encapsulate their own logic, state, and rendering,
making it easier to build and maintain complex user interfaces.
Virtual DOM: ReactJS uses a virtual representation of the DOM
(Document Object Model), known as the Virtual DOM. When the state of a
component changes, React updates the Virtual DOM.
JSX: JSX is a syntax extension for JavaScript used in React. It
allows developers to write HTML-like code within JavaScript, making it
easier to describe the structure and appearance of components. JSX code is
transpiled to regular JavaScript using tools like Babel before being
executed in the browser.
Hooks: React introduced Hooks in version 16.8 as a way to use state
and other React features in functional components. Hooks allow developers to
write reusable logic and manage state within functional components without
the need for class components. The most commonly used hooks are useState for
managing state and useEffect for handling side effects such as fetching data
or subscribing to events.
React Router: React Router is a popular routing library for React
applications. It enables developers to create single-page applications with
multiple views and handles routing between different components based on the
URL.
State Management: React provides a flexible ecosystem of state management
solutions. While React's built-in state management (useState ) is suitable
for managing local component state, more complex applications may benefit
from additional state management libraries like Redux. These libraries help
manage global application state and provide predictable ways to update and
access the state.
ReactJS has gained widespread popularity due to its performance,
reusability, and declarative approach to building user interfaces. It has
large community.
NOTE - Here we will Tailwind CSS for designing.
Here's an overview of how props work in React:
Parent Component: The parent component is responsible for defining
and passing the props to its child component(s).
Child Component: The child component receives the props passed from
its parent and uses them to configure its behavior and render content
dynamically.
Prop Passing: Props are passed as attributes to the child component
when it is rendered within the parent component's JSX.
Accessing Props: Inside the child component, the props can be
accessed as an object, and the values can be accessed using dot notation
(e.g., props.name, props.age, etc.).
Example:
Parent Component:
import React from 'react'; import ChildProps from './ChildProps'; function ParentProps() { const name = 'Kumar Atul Jaiswal'; const age = 24; return ( <div> <ChildProps name={name} age={age} /> </div> ); } export default ParentProps;
Child Component
import React from 'react'; function ChildProps(props) { return ( <div> <h1>Name: {props.name}</h1> <p>Age: {props.age}</p> </div> ); } export default ChildProps;
The ParentComponent renders the ChildComponent and passes two props: name
and age. The child component receives these props as an object (props) and
uses them to render dynamic content. The child component can access the name
and age props using props.name and props.age, respectively.
Props are read-only, meaning that the child component cannot modify the
props directly. They are used to provide information and configuration to
the child component from its parent. If the parent component needs to update
the data passed to the child component, it can do so by changing the prop
values in its own state and triggering a re-render of the child component
with the updated props.
And ParentProps component call in App.jsx
// import Profile from "./components/Profile" import { Route, Routes } from "react-router-dom"; import ParentProps from "./components/ParentProps"; export default function App() { return ( <> <ParentProps /> </> ) }
Output
Disclaimer
All tutorials are for informational and educational purposes only and have
been made using our own routers, servers, websites and other vulnerable free
resources. we do not contain any illegal activity. We believe that ethical
hacking, information security and cyber security should be familiar subjects
to anyone using digital information and computers. Hacking Truth is against
misuse of the information and we strongly suggest against it. Please regard
the word hacking as ethical hacking or penetration testing every time this
word is used. We do not promote, encourage, support or excite any illegal
activity or hacking.
0 comments:
Post a Comment
For Any Tech Updates, Hacking News, Internet, Computer, Technology and related to IT Field Articles Follow Our Blog.