Date: Dec 14, 2022

Hello and welcome to this blog I am writing this blog as a revision for react So today we are going to re-visit a topic in react call props

So the first question is why do we need props ?

How to pass props at a very basic level

<CustomComponent name="radhe-krishna" />

How to access props in a component ?

Consuming props in a Component

function CustomComponent(props) {
	return <div>{props.name}</div>;
}

do you know before react we having been using props in native html elements like anchor(href),img(src)

can we use custom props in native DOM elements ?

Basic Introduction Completed!

Advance Props Guide

Before learning more about props , we can have one small basic props exercise, so here you go

- open code sandbox (react environment)
- make a custom component call `Info.jsx`
- render the custom component in app
- pass basic details `firstname, lastname, age, city, profession` as a prop
- consume that information in `Info` component and display

I myself is doing this mini-exercise while writing this article 😃🥳

My solution for exercise

If you did it ? Well Done! you know props If got stuck with non-string props or destructuring no worries we will cover this together, continue reading 😊

A Friendly Water Reminder if you haven’t

Water Reminder

What did you learn new with that exercise ?

<Info
	firstName="Dheeraj"
	lastName="Purohit"
	age="23"
	city="Mumbai"
	profession="Developer"
/>

Non-String and Multiple Props

A) Non-String

<Info
	firstName="Dheeraj"
	lastName="Purohit"
	age={23}
	isIndian={true}
	city="Mumbai"
	profession="Developer"
/>

B) Multiple Props

export default function App() {
	const propsData = {
		firstName: "Dheeraj",
		lastName: "Purohit",
	};

	return (
		<div className="App">
			<Info {...propsData} />
		</div>
	);
}

javaScript info for spread syntax

PAY ATTENTION!

3) Consuming multiple Props and Object as a Prop

A) Consuming Multiple Props

export function Info(props) {
	const {
		firstName,
		lastName,
		age,
		city,
		profession,
		isIndian,
	} = props;

	return (
		<div>
			<ul>
				<li>{firstName}</li>
				<li>{lastName}</li>
				<li>{age}</li>
				<li>{city}</li>
				<li>{profession}</li>
				<li>{isIndian ? "Indian" : "Non-Indian"}</li>
			</ul>
		</div>
	);
}

B) Using Multiple Props (Object) without destructuring

export default function App() {
	const propsData = {
		firstName: "Dheeraj",
		lastName: "Purohit",
	};

	return (
		<div className="App">
			<Info propsData={propsData} />
		</div>
	);
}
export function Info(props) {
	return (
		<div>
			<ul>
				<li>{props.propsData.firstName}</li>
				<li>{props.propsData.lastName}</li>
			</ul>
		</div>
	);
}

Refactoring time

Ending Note!
- i might have missed some parts of props, but tried to put it here as a part of my revision
- if you find this article helpful, like, share with others who is learning react
- if you find any spelling mistake, wrong term used do let me know in comments and your feedback about this blog

Happy Coding
Jai Shri Krishna🙏💜