11 January 2020

Default props in functional component react

  • Create Components folder under src folder
  • Add PopsDefaultInFunEXP1.js file and add below Code
import React from "react";

function PopsDefaultInFunEXP1(props) {
return (
<div>
Default Pops In Fun EXP1
<p>{props.Name}</p>
<p>{props.Id}</p>
</div>
);
}

export default PopsDefaultInFunEXP1;
PopsDefaultInFunEXP1.defaultProps = { Name: "JC", Id: 101 };
  • Open index.js file and update code as below
import React from "react";
import "./styles.css";

import PopsDefaultInFunEXP1 from "./Components/PopsDefaultInFunEXP1";

export default function App() {
return (
<div className="App">
<PopsDefaultInFunEXP1 />
</div>
);
}


Output:

No comments:

Post a Comment