top of page
Logo der Online Agentur mdwp

Method

In React JS, a method is a built-in or user-defined function that performs a certain action. They are a set of instructions that perform a specific task for a React Component. Methods can be created inside the class component in React JS.

There are several built-in lifecycle methods in React, such as componentDidMount(), componentDidUpdate(), render(), etc., which serves specific purposes in the component's lifecycle.

Here is an example where a user-defined method "HandleClick" is defined:

```javascript
class MyComponent extends React.Component {
handleClick() {
console.log('Button clicked');
}

render() {
return <button onClick={this.handleClick}>Click me</button>;
}
}
```

In this example, handleClick is a method that gets executed when the button is clicked, and it simply logs a message 'Button clicked' to the console.

Apart from user-defined methods, there are standard built-in methods such as 'render()'. The render method returns the JSX to be rendered by the component.

```javascript
class MyComponent extends React.Component {

render() {
return <h1>Hello World!</h1>;
}
}
```
In this example, the 'render()' method returns an 'h1' element that says 'Hello World!'. This is the JSX that this component will display when it is used.

bottom of page