React JS
React JS
1. Props (Properties):
o Definition: Props are short for properties and are a mechanism for passing
data from parent to child components in React.
o Usage: Props are passed as attributes to components when they are being used
in JSX. They are immutable and read-only within the component that receives
them.
o Purpose: Props allow components to be customizable and reusable. They
enable dynamic behavior by allowing different data to be passed to the same
component.
o Example:
jsx
●
● // ParentComponent.js
● function ParentComponent() {
● }
● // ChildComponent.js
● function ChildComponent(props) {
● }
Elements:
● Definition: Elements are the building blocks of React applications. They are plain
JavaScript objects that describe what you want to see on the screen.
● Usage: Elements are created using JSX syntax or React.createElement() function.
They represent the structure of the UI but are not directly rendered to the DOM.
● Purpose: Elements represent UI components in a declarative manner. They allow
React to efficiently update and render the DOM when state or props change.
● Example:
jsx
2.
o // JSX syntax
o const element = <h1>Hello, world!</h1>;
o
o // React.createElement() function
o const element = React.createElement('h1', null, 'Hello,
world!');
o
Key Differences:
● Props are used to pass data from parent to child components, while elements represent
the UI structure.
● Props are immutable and read-only within components, while elements describe the
desired UI state.
● Props are passed as attributes to components in JSX, while elements are created using
JSX syntax or React.createElement() function.
In summary, props enable data flow between components, while elements define the structure
of the UI in React applications.