Conteúdo do curso
Immersion in React: Comprehensive Course for Beginners
Sobre a Aula

Data Transfer Between Components

Hello, students! In the previous topic, we learned about the React component lifecycle. Today, let’s learn about the data transfer between components.

Why Transfer Data Between Components?

In React applications, components often need to exchange data with each other.

For instance, a parent component may want to pass data to a child component so that the child component can render that data.

Or, a child component may want to send data back to the parent component so that the parent component can take action based on that data.

How to Transfer Data Between Components?

React components use props to transfer data between each other.

Props are properties that can be passed from a parent component to a child component.

Props are defined in the declaration of the parent component class using the propTypes modifier.

Example of Data Transfer Between Components

Let’s see an example of how to transfer data between components using props:

class App extends React.Component {
  state = {
    name: "John Doe",
  };

  render() {
    return (
      <div>
        <Header name={this.state.name} />
      </div>
    );
  }
}

class Header extends React.Component {
  render() {
    return (
      <h1>{this.props.name}</h1>
    );
  }
}

In this example, the App component has a state called name. The App component passes the value of the name state to the Header component as a prop called name. The Header component renders the value of the name prop in a header.

Conclusion

Props are a powerful way to transfer data between React components. They are an essential tool for creating complex and reusable React applications.

I hope you’ve understood how to transfer data between components.

Entrar na conversa
Rolar para cima