Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add style attribute in ReactJS component

Introduction to ReactJS

Welcome to the world of programming! In this blog post, we are going to discuss ReactJS – a JavaScript library widely used for building user interfaces, especially for single-page applications. Don't be intimidated by these technical terms. Think of ReactJS as a set of building blocks (components) that you can use to build a beautiful house (web app).

What are Components in ReactJS?

ReactJS allows you to create reusable UI components. These components, like building blocks, help you construct your webpage. Each component in React has its own structure, methods, and APIs. They can be as simple as a button on your webpage or as complex as an entire dashboard layout!

Understanding Style Attributes

Just as you would paint and decorate your house, in ReactJS, we can use 'style' attributes to add colors, dimensions, and more to our components. You can think of the style attribute as a decorator tool that React owns. It is a way of applying styles to components, similar to how CSS styles HTML.

Adding Style Attributes to a ReactJS Component

To add a style attribute in ReactJS, we use inline styling. But here's the catch: React does not use HTML-style strings for inline styles. Instead, it uses JavaScript Object Notation (JSON). JSON is a format for structuring data and is used in programming to transmit data between a server and a web application.

Here's an example of how you might style a button in HTML with CSS:

<button style="color: blue; margin: 10px;">Click Me!</button>

In React, the equivalent would be written like this:

<button style=>Click Me!</button>

But why does React use JSON for styling? There are a few reasons:

CamelCase syntax: HTML uses kebab-case (i.e., background-color), but in JavaScript, we use CamelCase (i.e., backgroundColor). By using JSON, we can keep the syntax consistent with JavaScript norms.

Scoping: Inline styles in React are scoped to the component. That means if you define a style for a component, it will not affect other components. This is like painting a room in your house: painting one room blue doesn't mean all the rooms will be blue.

Dynamic styling: With JSON, you can program styles to change dynamically, based on conditions or user interaction. This is like having a room that can change its own paint color based on the time of day!

Here's an example of how you can apply dynamic styling in React:

class MyComponent extends React.Component {
  render() {
    let style = {
      color: this.props.isClicked ? 'blue' : 'black'
    };

    return <button style={style}>Click Me!</button>
  }
}

In the above example, the button's text color will dynamically change from black to blue when the 'isClicked' prop is true.

Conclusion

Learning to code can feel like learning a new language, but with practice and patience, you'll be fluent in no time. The important thing is not to be afraid of making mistakes. Remember, every error is a learning opportunity!

In this blog post, we've learned about ReactJS, components, style attributes, and how to dynamically apply styles. We've also seen how React uses JSON for styling instead of traditional HTML strings. This approach provides a consistent syntax, scoped styling, and the ability to implement dynamic styling.

Remember, styling in React is like decorating a house. Each component represents a part of your house, and the style attribute is your decorating tool. So, go ahead and start decorating your web applications! Happy coding!