
Main Elements of React Components
- Component Definition:
- Functional Component: Defined as a function and created using
function
or anarrow function
. For example:
function Welcome(props) { return <h1>Hello, {props.name}</h1>; }
- Class Component: Defined as a class and uses the
class
keyword. For example:
class Welcome extends React.Component { render() { return <h1>Hello, {this.props.name}</h1>; } }
- Props:
- Used to pass data to components from the outside. With props, dynamic values can be given to components.
- Example:
<Welcome name="Sara" />
- State:
- Used to store the internal state of a component. State allows for changes within the component.
- Example:
class Counter extends React.Component { constructor(props) { super(props); this.state = { count: 0 }; } render() { return ( <div> <p>Count: {this.state.count}</p> <button onClick={() => this.setState({ count: this.state.count + 1 })}> Increment </button> </div> ); } }
- Lifecycle Methods:
- Methods related to the component's lifecycle, used during creation, updating, and removal of components. Available in class components, for example:
componentDidMount
,componentDidUpdate
,componentWillUnmount
.
- Rendering:
- Specifies how the component is displayed on the screen. In class components, rendering is done through the
render
method, or through thereturn
statement in functional components. - Example:
function MyComponent() { return <div>Content here</div>; }
- JSX (JavaScript XML):
- A syntax similar to HTML used in React components, which allows writing HTML-like code within JavaScript.
- Example:
const element = <h1>Hello, world!</h1>;
- Event Handling:
- Components respond to user actions (such as button clicks). Event handlers are added through attributes like
onClick
,onChange
. - Example:
function handleClick() { alert('Button clicked!'); } function Button() { return <button onClick={handleClick}>Click Me</button>; }
Conclusion
Similar Articles
Main features of Reactjs
In this article, we will learn about the key features of React.js, which will also be useful for interview questions.
August 21, 2024React Native: Why Choose React Native for Mobile Development?
The field of mobile development is rapidly evolving, and developers constantly seek efficient ways to create applications for both iOS and Android platforms simultaneously. React Native has emerged a
December 15, 2024Do you love React? Then Next.js, built on top of React, will amaze you
The limitations of old stacks, today’s traffic–cybersecurity requirements, and the new rules of the SEO game have changed. Next.js brings speed, security, and structured management together in one fra
August 27, 2025What is React js? What role does React play in software development?
Today, we'll discuss React.js, one of the top JavaScript libraries, including its advantages, what we can do with it, and everything else you need to know in this post.
August 20, 2024What is an SPA (Single Page Application)?
Let's explore what an SPA (Single Page Application) is, which is used in today's modern websites.
August 29, 2024Advanced React.js File Structure
In this article, we'll delve into creating a file structure that enhances usability, scalability, and clarity, ensuring your complex applications remain well-organized and easy to manage. We'll discus
August 25, 2024