Components with Props In React

in react •  7 years ago  (edited)

Components in React behave like functions. They can take in arguments and returns values.

Components take in Props (like arguments) and returns some piece of the UI for the app.

Props are objects and can have any data type as a property: number, strings, objects, functions, and even other components.

react components like functions.png

When we use a component we can pass in the props using the attribute on the JSX component. For example,

< Alert message="You need to login before you do that" />

Here, Alert is our component and we are passing the Props of 'message'.

We access that message inside our component definition using Props.message. This will equal the string "You need to login before you do that".

Props.message is "You need to login before you do that".

A few notes about Props

Props – are read-only, this is because components act like ‘pure’ functions. Pure functions are functions that don’t change their inputs. Their return value is only determined by its input values.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @positivelyzack! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

You made your First Vote

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

Upvote this notification to help all Steemit users. Learn why here!