Understanding Props and PropTypes in React | Complete React Course in Hindi #6

CodeWithHarry
17 Aug 202126:03

Summary

TLDRIn this video, the instructor demonstrates how to work with React props by creating a custom Navbar component. They explain how to pass properties (props) to components, set default prop values, and enforce prop types using PropTypes. The video also covers the importance of organizing code, using extensions for efficiency, and understanding JavaScript export concepts. The focus is on building a clear and maintainable React application, with an emphasis on learning and implementing essential React concepts.

Takeaways

  • 1. The video initializes a TextUtils website using Bootstrap and frequently commits code changes to track progress.
  • 2. Git commands such as `git add .` and `git commit -m` are used to commit changes; familiarity with Git is not required to follow along.
  • 3. The video focuses on creating and understanding React components, specifically function-based components and the concept of props (properties).
  • 4. Props are used to pass data to custom components, making them dynamic and reusable.
  • 5. The video demonstrates the use of the ES7 React/Redux/GraphQL/React-Native snippets extension to speed up coding with shortcuts like `imr` for imports.
  • 6. The importance of keeping comments in the create-react-app template is emphasized, as they contain useful information.
  • 7. The video explains the difference between named exports and default exports in JavaScript modules.
  • 8. React component creation is demonstrated, including moving code to a separate Navbar component for cleaner structure.
  • 9. Prop types and default props are introduced to define expected types and default values for props, ensuring component robustness.
  • 10. A brief overview of state management is provided, with a promise to delve deeper into the concept in future videos.

Q & A

  • What is the purpose of making frequent commits in the video?

    -Frequent commits are made to keep track of the code changes after each video, making it easier to reference specific versions of the code related to each video segment.

  • What is a 'prop' in React, and how is it used?

    -A 'prop' (short for properties) is a mechanism for passing data from a parent component to a child component in React. It allows components to be reusable by customizing their behavior or appearance based on the data passed to them.

  • How do you add Bootstrap to a React project as mentioned in the video?

    -Bootstrap can be added to a React project by including it in the project’s dependencies and importing it in the appropriate files. The video doesn't detail the exact steps, but it involves modifying the `app.js` to use Bootstrap classes.

  • What is the ES7 React/Redux/GraphQL/React Native snippets extension, and why is it recommended?

    -The ES7 React/Redux/GraphQL/React Native snippets extension provides shortcuts for commonly used code snippets in React development, making coding more efficient by reducing the amount of repetitive typing.

  • How do you import modules in JavaScript, and what are named and default exports?

    -Modules in JavaScript can be imported using either named exports or default exports. Named exports allow you to export multiple items from a module by name, while default exports allow you to export a single item without naming it, making it the default export of that module.

  • How can you create and use a custom React component?

    -You can create a custom React component by defining a function that returns JSX. This component can be used in other parts of the application by importing it and including it in the JSX of another component.

  • How do you pass props to a React component, and how are they accessed within the component?

    -Props are passed to a React component by including them as attributes in the JSX tag. Inside the component, props are accessed through the `props` object, typically by using `props.<propName>`.

  • What are prop types in React, and why are they useful?

    -Prop types in React are used to specify the expected types for props that a component should receive. They help catch bugs by providing type-checking and ensuring that components receive the correct type of data.

  • What are default props in React, and how do you set them?

    -Default props are fallback values for props that a component can use if no value is provided. They are set using the `defaultProps` property of the component.

  • Why is it important not to modify props directly in a React component?

    -Props should be read-only and not modified directly because they are meant to represent data passed from a parent component. Changing them directly can lead to unexpected behavior and makes the component's behavior harder to predict.

Outlines

00:00

πŸ“¦ Initializing Textutils with Bootstrap and Git

The speaker reviews the initialization of the Textutils website with Bootstrap and demonstrates Git commands (`git add .` and `git commit -m`) to manage versions of the code. The video aims to explain 'props' in React, a shorthand for properties, which allows components to receive data. The speaker shows how to create a custom component and install the ES7 React/Redux/GraphQL/React Native snippets extension to simplify coding with shortcuts.

05:01

πŸ“š Understanding Default and Named Exports in JavaScript

This section covers JavaScript module exports, focusing on default and named exports. The speaker explains how to create constants and export them, illustrating the difference between exporting by default and named exports. They run code examples, highlighting common errors and solutions, like using `.mjs` for modules. The importance of understanding these concepts before diving deeper into props is emphasized.

10:05

πŸ”„ Creating and Using a Navbar Component with Props

The speaker demonstrates creating a Navbar component in React, explaining how to pass and use props within it. They emphasize organizing code by moving component-specific code out of `App.js` into its own file. The example shows passing a title to the Navbar component via props, illustrating how props can make components reusable with different data. The importance of referring to official React documentation is also mentioned.

15:07

πŸ“ Prop Types and Default Props in React

This section explains how to use PropTypes in React to define expected prop types and default props to set default values. The speaker demonstrates how to specify prop types and make props required, showing how this can prevent errors by ensuring components receive the correct data types. They illustrate setting default props and emphasize the importance of defining prop types for maintaining code quality and preventing runtime errors.

20:09

πŸ”§ Advanced Prop Handling: Default Values and Prop Validation

The speaker delves into setting default prop values and using the `isRequired` attribute to enforce prop requirements. They show examples of how missing required props result in errors, which can be mitigated by setting default values. The importance of prop validation for ensuring components function correctly and as intended is emphasized, along with the usage of default props for fallback values.

25:10

πŸ“˜ Conclusion and Encouragement to Explore More

The speaker concludes the video by addressing the perceived time investment in building the app, emphasizing the learning value of each small concept. They encourage viewers to explore more videos in the playlist, practice the concepts covered, and appreciate the step-by-step learning approach. The video ends with a thank you note and an invitation to watch the next video.

Mindmap

Keywords

πŸ’‘Bootstrap

Bootstrap is a popular front-end framework used to create responsive and mobile-first websites. In the video, Bootstrap is added to the textutils website to improve its appearance and functionality by providing pre-designed components and CSS styles.

πŸ’‘git add .

The command 'git add .' is used in Git to stage all the changes in the working directory for the next commit. This is mentioned in the video as a way to prepare the code for committing, ensuring that all changes are included in the version control history.

πŸ’‘git commit -m

The command 'git commit -m' is used to commit staged changes in Git with a message describing the changes. In the video, the instructor commits the code with the message 'Video 5 completed' to mark the progress of the tutorial series.

πŸ’‘props

Props (short for properties) are a way to pass data from a parent component to a child component in React. The video explains that props allow customization of components by passing different values, like a title or boolean, which the component can then use to render itself.

πŸ’‘React

React is a JavaScript library for building user interfaces. The video focuses on teaching concepts related to React, such as creating components, passing props, and using various extensions to streamline React development.

πŸ’‘npm start

The command 'npm start' is used to run the React application. It compiles the code and starts a local development server, allowing the developer to see changes in real-time. The video demonstrates running this command to view the website in the browser.

πŸ’‘ES7 React/Redux/GraphQL/React-Native Snippets

This is a Visual Studio Code extension that provides shortcuts for common React and Redux code snippets. The video highlights its usefulness in speeding up development by auto-generating code patterns, such as imports and component structures, with simple abbreviations.

πŸ’‘default export

Default export allows a single value to be exported from a module in JavaScript. The video explains this concept using examples, showing how to export a constant by default and import it in another module, emphasizing the importance of understanding default exports in JavaScript.

πŸ’‘named export

Named export allows multiple values to be exported from a module in JavaScript. The video contrasts this with default export, demonstrating how to export and import multiple constants using named exports, and explaining the syntax and use cases for each.

πŸ’‘prop types

Prop types are a way to specify the expected types of props in a React component. The video shows how to use the 'prop-types' library to enforce type checking, ensuring that the component receives props of the correct type, and highlights the importance of this for catching bugs early.

πŸ’‘default props

Default props are values that are used when no props are provided to a React component. The video demonstrates how to set default props for a component, ensuring that it has fallback values and behaves correctly even if some props are not passed.

Highlights

Initialization of textutils website with Bootstrap.

Explanation of git commands: `git add .` and `git commit -m` for version control.

Introduction to props in React and how they are used in custom components.

Using npm to start the project with `npm run start` or `npm start`.

Importance of keeping comments when using create-react-app for useful information.

Demonstration of default and named exports in JavaScript modules.

Creating and using a new React functional component (Navbar) and passing props to it.

Using the ES7 React/Redux/GraphQL/React Native snippets extension for code shortcuts.

Setting up prop types and default props for React components to ensure proper data types.

Explanation of the significance of using default props and required props in React components.

Introduction to the concept of state in React (to be covered in later videos).

Emphasis on the importance of understanding props before moving to more advanced concepts.

Example of passing and utilizing props such as strings and objects in components.

Detailed process of creating organized and reusable components in React.

Encouragement to refer to React documentation for understanding syntax and best practices.

Transcripts

play00:00

So, in previous video we have initialise textutils website

play00:04

by adding Bootstrap. I'll open it with code again

play00:08

And I will do one thing which you don't have to think about

play00:13

Is that I will add git add .

play00:15

and I'll write git commit -m "Video 5 completed"

play00:23

So this is the code when our video 5 was completed

play00:27

So I will make this kind of commits again and again

play00:31

So that you get code after which video which code we have written

play00:36

Hence I am making so many commits. Many will ask what is this

play00:38

git add . and git commit -m what is this?

play00:41

you don't have to see this for now.

play00:43

if you don't know git, then you can learn react without git too

play00:48

I wrote this because I need to give the code to you.

play00:50

Now, as we know we are editing in app.js

play00:56

We'll talk about props in this video

play00:59

What is props? Props is short for properties

play01:01

Overall, if I make a custom component

play01:04

Which I am going to make right now

play01:06

I can send some things to a custom component

play01:10

Which it can use inside it

play01:12

For example what can I send inside navbar?

play01:15

I will quickly do npm Run start

play01:19

or npm start and will wait till it up

play01:23

So here it is compile successful and I can see my website here too

play01:28

So if I make a custom component

play01:31

And suppose that custom component is navbar

play01:34

Then I can send this string textutils

play01:36

An object or a Boolean

play01:39

And using all these values component will render itself

play01:43

I will show you this in a while if you didn't understood

play01:46

Before that I want to tell you we have installed an extension

play01:49

Which was ES7 react/redux/graphQL/react native snippets

play01:53

This extension is superb

play01:55

in a way that here many shortcuts are given

play01:59

if you write imp then this will come

play02:02

imn will give this

play02:04

so if you stick this extension and there are many more like this

play02:09

for example, simple react snippets, if i am not wrong

play02:13

but i feel this the best and use this only

play02:15

So, you will learn all this thing

play02:18

for example, you want to add prop types, I will tell you what that is

play02:22

but if you want to use, or want to import react

play02:24

imr will do that, you don't need to write this whole.

play02:28

with that class based components or

play02:32

function based component all these things

play02:36

if you want to create, see for prototype write pta, if you write ptar

play02:40

array and is required will come too, if you write ptpr

play02:43

prop types Boolean and is required will come.

play02:46

so you will remember all these things.

play02:49

and you can do things easily.

play02:52

there are many things for ES7 too in this

play02:54

like for console.log write clg, it will come

play02:57

and whatever you are using just go through it once

play03:02

for example, you are writing console.log as whole

play03:04

don't write whole, write clg

play03:06

oops where I clicked?

play03:08

write clg and see this once what are mentioned in this,

play03:13

and the extension here,

play03:16

if you search it on net too, I clicked here directly

play03:20

and here all code of its is given which you can see

play03:24

you don't need to see in VS code you can see here too

play03:27

and there are many things written in it of react's

play03:30

class based component, react's function based component.

play03:33

there are many things which you can't mug up but once you go through

play03:37

on writing hoc redux you get this whole thing.

play03:41

imagine the time you save while typing a component.

play03:44

if you are using this.

play03:46

This you have to keep in mind.

play03:48

This is the one thing, one more thing which I want to tell you is

play03:51

That whenever you are using create react app

play03:54

Means the utility which we used

play03:57

To create this whole folder structure If you are using that

play03:59

Then try to not remove the comments

play04:02

Because that comments contain Useful information

play04:05

Suppose if you are making this app as tenth react app

play04:09

Then you might know what is written in comment and you can remove them

play04:12

But go through it once there are many useful things written in comment

play04:16

I have removed because I have already told you all those thing

play04:18

But just in case if you want to read those comment do read

play04:21

With that one more thing which I want to tell you is

play04:25

There is no need to import react now

play04:27

Example if I write Imr here

play04:29

Then I can import react but its not needed

play04:31

Because I am using function based component

play04:33

And I'm not using react in it

play04:36

So this is what you should know

play04:38

One more concept which I wanted to tell you

play04:40

Which is a concept of JavaScript

play04:42

Is of named export and Default export

play04:45

And to understand this is very important

play04:47

Before I move ahead and tell you about props

play04:50

It is necessary to understand this

play04:51

And I can't ignore all these things

play04:53

If I don't tell you this then there will be problem

play04:56

Hence understand this

play04:58

Suppose I created a file named module1.JS

play05:01

And with that a file named module2.JS

play05:04

And then later on I delete this both module is because we don't want it

play05:07

Suppose I have written here const a ="Harry";

play05:12

and here const b="Rohan";

play05:17

and with that here const c ="Akash";

play05:21

and with that here const d ="Priyanka";

play05:28

So I created this four constants here

play05:30

Now if I write here export default c;

play05:36

import ui from module then you will ask what this ui is?

play05:45

I will write it like this ./ means inside current directory.

play05:51

and I'll write here module2

play05:53

Here I am basically saying that from module2

play05:56

Import UI and I will write here console.log(UI);

play06:00

Now if I try to run it

play06:02

Then I will get an error and we will talk about it too

play06:04

But for now I want to run it and click on +

play06:08

I will write here node and I will run module1 here

play06:12

On pressing enter there will be an error which will say

play06:14

Either you make type = module in your package.JSON

play06:18

or you make it MJS

play06:20

Means I will make it module1.mjs and module2.mjs

play06:25

So what I will do by making it MJS that

play06:29

If I mport it now then my import will work so I will make it mjs quickly

play06:32

Now if I run it then it will run successfully

play06:35

So, it is saying module not found

play06:38

What it is saying module1.js, so I have to write MJS here too

play06:42

So I will write MJS and as its name is changed

play06:45

it is module1.mjs, so if I run it then

play06:47

Akash is being printed here,

play06:49

Because I exported Akash by default

play06:52

Suppose if I write d here then we will get Priyanka why

play06:59

Because Priyanka is being exported by default

play07:04

Priyanka have to come as a she is exported by default

play07:06

I have written here export default d so it have to come to module1

play07:12

Now if I write here export default b

play07:15

Then I will get Rohan here you have to know this

play07:19

And this is true you can see here Rohan

play07:22

So this was default export now I will show you what is named export

play07:25

Here by default and exporting b which is Rohan

play07:29

so if I write here import dz from module2.MJS

play07:34

Then I will get Rohan printed on screen

play07:38

Because by default that is being exported if I write dza here

play07:43

so if I call it by name of dza too then also it is by default exporting it

play07:46

If I want to export A,C and D too then how I will do it?

play07:50

I will write export {a}

play07:54

And C and D too here.

play07:56

If I want to bring A, C and D here

play07:58

Then it is not being exported by default

play08:00

I have to bring it here so what I will do

play08:03

Suppose I want to bring dza too and A, C and D too

play08:10

Then I will bring something like this a, c and d

play08:12

And if I do console.log C and D

play08:18

And will run it so you can see Akash and Priyanka too

play08:22

I will write a too so Harry will print too

play08:26

Now here I have to say it as a only if I write A1 I will get error

play08:32

Because it is not being exported by default there is no a1 so it shows error

play08:37

There is no A1 in module2 so this is what it is saying

play08:41

Now if I want I can do only a, c and d

play08:45

But I have to use {} when I am using which is not exported by default

play08:53

This is how module works if you feel it confusing

play08:56

So it is basically a JavaScript concept

play08:58

And if you are facing problem you can go back in video and watch

play09:03

But in most of the cases you won't face the problem

play09:06

So what I will do is, module1.mjs and module2.mjs

play09:10

I will keep them only for this video and will delete for next video

play09:14

I want to keep them just for your reference

play09:17

I hope you have understood module

play09:19

Now we will understand props for which we will make a new component

play09:24

And this is our first react component so have sweets

play09:30

Will make a folder named components, folder is not needed

play09:35

But by making folder we can find our custom components at one place

play09:39

And things stay organised

play09:41

I will write components here and after that I will make component

play09:45

You have to always write C capital in component like in navbar.JS

play09:50

Written N capital of Navbar

play09:55

and always write it in capital.

play09:58

Now listen carefully what you need to do

play10:01

I need react functions based component I'll write rfc

play10:05

And my react function based component came here

play10:09

Now I can write my coding here like I've written hello I am navbar

play10:14

Suppose I wrote this and what I will do here

play10:18

I'll write navbar anywhere in fact after navbar I will right navbar

play10:23

Navbar as I have written it got imported

play10:28

it found automatically navbar is inside components

play10:32

and it Imported automatically

play10:34

I will write something like this let me show you by zooming

play10:40

Navbar, now see what changes have come in my website

play10:43

Hello I am a navbar came here

play10:45

Which means the component I made is loading

play10:48

You should understood this

play10:50

But why I made Navbar component?

play10:52

I made it because the coding here don't get messy

play10:55

My whole application will be seen in my app.js

play10:59

I don't want the whole navbar to be Messy in this

play11:02

So what I will do is cut this navbar code

play11:06

I will make this navbar as navbar which means

play11:10

I will make it a navbar which I can use in future too

play11:15

So I cut it I will come in navbar.JS

play11:17

I will remove hello I am a navbar and div

play11:20

And I will return this nav

play11:24

So my navbar is returning this

play11:26

And here in my app I am only returning navbar

play11:30

So my app is clean and it shows I am importing navbar

play11:37

So you can see here nothing changed

play11:39

Now I can use this navbar in another website or in the same website

play11:44

But with some other title for example

play11:47

If my website is a blog I will write blog

play11:51

Suppose I want to make textutils blog instead of textutils

play11:56

So I want to pass it like a variable

play11:58

I want to use this component

play12:00

But keep some things which changes according to me in this component

play12:05

For example let me show you then you will understand

play12:09

I want I pass textutils here

play12:13

Will pass textutils like this I want to write

play12:16

Title = textutils

play12:19

I want to pass textutils something like this

play12:23

Is this possible?

play12:25

If I write title=textutils something like this

play12:29

So basically I am passing props

play12:32

I hope you understood props

play12:34

I will open documentation of react again here

play12:36

And this is the source

play12:38

If someone find this boring then change your opinion

play12:43

This is not boring you have to come here only

play12:47

This is the source of the information on react

play12:52

So I am searching props here of components

play12:55

So basically our function based component will get this props

play12:58

So I will accept this inside this function

play13:02

I'll write props and I will get this props here

play13:05

Now I can write here props.title

play13:08

Obviously this is JavaScript so I won't write directly props.title

play13:11

will write inside {}

play13:13

will write props.tile inside {}

play13:16

And by doing this, this title will come here lets see.

play13:20

See textutils, you won't see difference I will show you by doing textutils2

play13:25

If I make it textutils2 like this then it will show textutils2

play13:30

It means I made a react component of which I can change title

play13:35

Means if I need to make another website

play13:37

Then I can utilise this navbar by changing title

play13:41

or if I want to change any other thing I can pass it using prop

play13:45

And will accept their by using props.

play13:49

Let me show you an example of about text

play13:53

Suppose I want to write it by using props.aboutText

play13:57

will make T capital of text for clear understanding

play14:02

If I write aboutText something like this

play14:06

I want to write about us or about textutils instead of about

play14:15

And I will change this textutils2 to textutils

play14:18

So I am using aboutText here

play14:21

Props.aboutText so will it change here?

play14:25

About textutils yes will definitely change

play14:27

So what is props? nothing else but properties

play14:30

You made a component and you want to pass some things in it

play14:35

You say that this is the component with some variables in it

play14:38

Which I will tell what it will be is props

play14:41

Understood what props is? props is

play14:44

That you are giving some things to this component

play14:48

This component needs these 2 things

play14:50

You are giving it that two things and asking it to render

play14:55

For example if I give your blank paper then it is a blank paper

play15:00

But if I give you some information in that blank paper

play15:04

Then that can be an application an leave application

play15:06

If I write job application in it, it can be job application

play15:09

So it becomes a useful thing because it has that information

play15:12

Which is required without which it is a blank paper

play15:16

Same way the component is empty without this two required props

play15:23

Because without title and aboutText this component is incomplete

play15:27

So I hope you understood this thing

play15:30

Textutils the text here came here

play15:32

About textutils this text came here,

play15:34

its not compulsory that this needs to be string

play15:36

I can pass an object too

play15:38

I can pass anything even a link

play15:41

It can be I can pass about's link

play15:43

and Use this about link here

play15:45

Prop.aboutLink I can send as many props as I want

play15:48

And if there are many props I can group them in object and pass object

play15:53

Then render those objects value

play15:55

So this is props you will never change the props in this function

play16:00

This is even written in react documentation let me show you

play16:04

You will not change props directly

play16:06

Props should be read only, at any cost

play16:10

You won't change props in your function

play16:14

Overall if you set props once it will stay same always

play16:20

But its not compulsory the text will stay the same

play16:23

And here a concept of state comes which I will tell you later

play16:27

Not right now because I want you to learn about props properly

play16:31

Now because of props concept

play16:33

you have to learn about props type concept too

play16:36

So what I will do this function is named navbar

play16:38

I'll write here navbar.proptype.

play16:44

And first of all I will import proptype

play16:47

So it's impt according to me so import proptypes impt

play16:52

import proptypes from proptypes

play16:54

now I'll write here navbar.proptypes =pts

play17:09

pts is proptype.string

play17:12

and I will put it in an object like this.

play17:17

And it is going to be an object so first of all

play17:20

I was set prototype for my first proptype which is

play17:25

title. So, I'll say my title proptype should be string

play17:30

With that I will say my second proptype which is

play17:33

AboutText I want it to be proptype.string too

play17:38

So this is an object. suppose you forgot the syntax

play17:42

You don't remember how to write function based component are different

play17:48

to write and class based component are different to write

play17:54

So let me tell you search here react proptype

play17:56

Everything is inside react JS documentation

play18:00

Let me show you where the syntax is written

play18:03

This is the syntax they imported proptype first

play18:06

Then the name of function means the name of function based component

play18:11

.proptypes = this they have written

play18:12

Never make this P capital like

play18:15

I did capital here, we have to keep it small

play18:18

Now I will save it in show you one thing

play18:20

See if I inspect it there is no error

play18:23

And why there should be you must be thinking

play18:25

But imagine if my prop here

play18:28

I send it like a number suppose like

play18:31

Title=3

play18:34

Suppose I send a number in title

play18:36

Then see it is saying failed proptype

play18:38

Invalid prop title of type number

play18:40

It is saying you mention the proptype of it as string

play18:44

And you are sending number

play18:45

Your dog is Tommy my dog is dog

play18:48

So I will change it back to string

play18:50

I will come back here and make it string

play18:52

I will make it back to textutils

play18:57

And will save it

play18:59

Now this error will go when I reload it

play19:02

You will ask where we do it we will never send number

play19:06

I will always send string so why this

play19:09

Why this is needed this long coding

play19:12

you won't send it you are saying it now

play19:15

But later you will send an ask why error is coming

play19:18

You will send object instead of string

play19:21

And then you will say I send object by mistake I need to send string

play19:26

And you won't be able to track your mistake

play19:30

Hence we use this to make a check beforehand

play19:32

You say the proptype here

play19:35

The title will remain .string

play19:38

AboutText will be string too. if it was array I would write array

play19:42

If it was an object I would write object

play19:46

If it was number I would write number

play19:49

So react documentation have written about proptypes properly

play19:52

Now there is one more thing defaultprops

play19:55

Now how will you write defaultprops in your function components

play19:58

Let me show you that too

play20:00

Now what will happen in function based component

play20:02

What you can do if you can set default prop

play20:06

You can set default prop like this and say

play20:09

That make something of my default prop value

play20:12

Let me show you can write something like this

play20:14

Navbar.defaultprops and the title suppose

play20:18

I want it to set title here

play20:21

And I want to write about text here in aboutText and I'll save it

play20:32

Then what will happen right now you are saying textutils and about textutils

play20:35

Because we have passed values

play20:37

But if I don't pass this value I will show this by ctrl+d

play20:41

I will comment this out

play20:43

and will remove this both things which I have passed

play20:46

And if I save it you can see about text here and set title here

play20:51

So what I did is I set default prop

play20:53

What I say this I will pass prop

play20:58

But if I don't pass the prop

play21:01

At that time use this values

play21:07

now I have set default prop here

play21:09

I can do one more thing I can use is required here

play21:13

I will do is required here and after that for title

play21:17

Right now I am sending and no error showing I will show you by inspect

play21:20

If I did is required and I didn't send it afterwards

play21:24

Let me show you suppose I didn't send it

play21:27

So error will come here

play21:28

we have set defaultprops then error will not come

play21:31

we have to remove it for seeing error

play21:33

Now title is required but see here

play21:37

I will reload it and it is saying you have marked title as required

play21:42

But this is undefined but if I set default prop and then load

play21:47

Then that error is gone because by default it got prop

play21:50

So it is satisfied and it got is required

play21:55

Means it should get some title it shouldn't be undefined

play21:57

It will show an error if it is undefined

play21:59

So if you want to ensure that your props

play22:02

Shouldn't be undefined for example title

play22:06

Title is important about can be undefined there is no problem

play22:10

If I want this then I will add is required in title

play22:13

By the way in most of the props do add is required because

play22:17

In most of the component there are things which you want to use

play22:23

Without them your component will be incomplete

play22:26

Like without title and aboutText this navbar component is incomplete

play22:30

So if I reload it set title here and this has come

play22:33

Because I am not passing anything

play22:36

So I will pass title and about text both

play22:39

If I pass then here textutils and about textutils came

play22:42

And my console is happy too not showing anything red

play22:46

No error is coming I will close this now

play22:49

So we understood what props is for now

play22:51

Props is nothing but we are basically passing values

play22:54

We are passing it from here and this component is accepting from here

play22:58

Prop.aboutText and with that props.title

play23:03

Where is title I can find it yes props.title

play23:06

So you can send props like this it can be objects too

play23:10

We also learnt about proptypes and defaultprops

play23:13

Proptypes tell us the types of prop

play23:18

If you try to use any other type then that you will see an error in console

play23:22

You will see a warning in red colour

play23:24

It will tell you even if you do it is required

play23:27

That it was required and you haven't do it so fix your code

play23:30

React will tell you that

play23:31

This is proptypes and what is default prop means

play23:36

Which value to use if nothing is passed.

play23:39

For example if I write this then which value will be used for Title and aboutText

play23:44

These two values will be used

play23:46

As you can see but in most of the cases when you are using component

play23:50

Then you will write something like this

play23:52

If you want to set another default title or aboutText or any other prop

play23:57

or if I say in broad terms defaultprops

play24:01

Should be set to something always and in most of the cases

play24:03

It will be useful for example any about section or title

play24:08

Then I will not change aboutText I will only write about

play24:11

or about us or only about

play24:13

And if I don't send about my work will be done I will do it

play24:18

And if I don't send about I will do ctrl+d

play24:21

I will do it backspace and comment out and like this

play24:25

Basically if you want to refer the code later

play24:29

So it took about from defaultprops type

play24:34

I have given in defaultprops

play24:37

I have given this in Navbar.defaultprops

play24:42

I had sent title so it took my title which was

play24:45

Textutils which I sent

play24:47

Now this thing should be clear to all

play24:49

If it's not means you were sleeping when I was making video

play24:52

You don't have to sleep you have to see

play24:54

By going back again if you have not seen properly

play24:57

And if you have seen then you will know

play24:59

I don't think anyone will face problem

play25:00

Don't think about state right now I will tell you later

play25:04

You just have to learn props in this video

play25:07

I hope you have become the master of props

play25:09

In the coming video we will see how they will take this app forward

play25:13

I know you will think so much time is taken in making such a small app

play25:18

This will be our first app in which we will take so much time

play25:21

We are learning small concept with the help of this app

play25:24

So you might feel a lot of time is being taken

play25:27

Do access playlist

play25:28

There will be many videos when you will accept this playlist

play25:32

So watch them and try this

play25:34

Go to the channel playlist and you will see many things

play25:38

That's it for this video

play25:40

Thank you so much for watching this video

play25:42

And I will see you next time

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
ReactJavaScriptWeb DevelopmentComponentsPropsProptypesCodingTutorialProgrammingFrontend