Prop is basically a short form of Properties. Generally we assume that every component that we create may have some sort of properties. These properties are defined in a certain way so that these can be transferred to another component. In short, Props are a kind of inter-component communication system.
Let us understand it with an example:
Consider you are creating a page where five cards are to be displayed with different information in each.
As discussed earlier, Props are simple properties that influence the way a component is rendered on screen. Let us now create a component that will get some props passed by another component.
Index.js
import React from 'react';
import ReactDOM from 'react-dom';
import Second from './Second';
import reportWebVitals from './reportWebVitals';
ReactDOM.render(
,
document.getElementById('root')
);
reportWebVitals();
Second.js
import React, {Component} from 'react';
import First from "./First";
class Second extends Component {
render(){
return(
);
}
}
export default Second;
First.js
import React, {Component} from 'react';
class First extends Component {
render(){
return(
Hi! {this.props.attr}
);
}
}
export default First;
Here we see that there is a file Index.js that is rendering the component
on screen.
Another file Second.js (working here as parent) holding the definition of component
Finally First.js (working here as child) is utilizing that prop for UI by using this.prop.attr statement.
NOTE: While using functional components, we utilize props by using prop.key statement whereas while working on class components, we use this.prop.key statement.