Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to display a message in ReactJS

Getting Started with Displaying Messages in ReactJS

ReactJS is a popular JavaScript library for building user interfaces, particularly for single-page applications. In this tutorial, we'll focus on a common task in ReactJS - displaying a message. Consider this as a friendly chat between two people, where ReactJS is the messenger who passes your message to the web browser.

Basic Message Display

In React, you can display a message by creating a React component. Think of a component as a factory that produces the same product but with different specifications. In our case, that product is a message. Here's how to create a simple component that displays a message:

import React from 'react';

class Message extends React.Component {
  render() {
    return <h1>Hello world!</h1>;
  }
}

export default Message;

In this code, we have a class named Message that extends React.Component. render() is a method that tells React what to display on the screen when this component is used. It's like instructing the factory what to produce.

Add Dynamic Content

Now, let's say we want to customize the message. We can do this by using props. Think of props as the ingredients that you provide to the factory to customize the product.

import React from 'react';

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

export default Message;

Now, when we use the Message component, we can pass in the text prop to customize the message:

<Message text="Hello, React learner!"/>

User Interaction

What if we want to change the message based on user interaction? Well, we can do that too! For that, we need to understand state. If props are the ingredients, state is like the secret sauce that can change the taste of the product over time.

import React from 'react';

class Message extends React.Component {
  constructor(props) {
    super(props);
    this.state = { text: 'Hello, React learner!' };
  }

  changeMessage = () => {
    this.setState({ text: 'You clicked the button!' });
  }

  render() {
    return (
      <div>
        <h1>{this.state.text}</h1>
        <button onClick={this.changeMessage}>Click me</button>
      </div>
    );
  }
}

export default Message;

In this code, we introduced a button element, and an onClick event that triggers the changeMessage function. This function updates the state, and therefore changes the message when the button is clicked. It's like the factory instantly producing a new product when the user requests it.

Conclusion

Congratulations! You've just learned how to display and customize messages in ReactJS. Remember, ReactJS is just a messenger passing your instructions to the web browser. The render() method tells what to display, props allow you to customize the content, and state lets you change the content over time.

In your journey as a React developer, you will encounter many more such concepts. But do not be overwhelmed. Just like learning to cook, it's all about understanding the recipe (code structure), ingredients (props), and the secret sauce (state). Keep experimenting, and you'll be a master chef (or rather, a master React developer) in no time!