Selected topic
React Components
Prefer practical output? Use related tools below while reading.
function keyword.this context: Unlike class-based components, functional components do not have access to the this context.componentDidMount, componentWillUnmount, etc.jsx
import React from 'react';function MyComponent() {
return <h1>Hello, World!</h1>;
}
In this example, we define a simple component called MyComponent as a function. This function returns an <h1> element with the text "Hello, World!".
jsx
import React, { useState } from 'react';function MyComponent() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
In this example, we use the useState Hook to create a state variable called count and an increment button that updates the count when clicked.
I hope this summary helps! Let me know if you have any questions or need further clarification.