DEV Community

Cover image for React Clicks, Styles, & Conditionals! 🎨💥
mayowa-kalejaiye
mayowa-kalejaiye

Posted on

React Clicks, Styles, & Conditionals! 🎨💥

🎉 Hello React Enthusiasts! 🎉

Welcome back to Day 2 of my React journey! Today, we’ve officially moved from the basics to the good stuff—we're talking click events, styling, and conditional rendering. 🖱️✨

You know, the magic ingredients that turn a static page into a dynamic, interactive app that feels... well, alive. Let’s dive in!

🚀 Outline: Building an Interactive React App (The Fun Stuff)

1️⃣ Adding Markup: Setting the Stage

Before we add all that juicy interactivity, we need a little structure. Here’s our humble beginning:

import React from 'react';

function App() {
  return (
    <div>
      <h1>Welcome to React Reloaded! 🚀</h1>
      <p>Day 2: Clicks, Style, and Conditionals. Let’s make things happen!</p>
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

“Simple, right? But hold on, things are about to get spicy!” 🔥

2️⃣ Styling: Bringing it to Life 🌈

Ever tried adding style to a button and felt like a wizard casting spells? 💫 No? Well, it's time to learn!

💥 Inline Styling
Get up close and personal with your buttons with inline styles:

const buttonStyle = {
  backgroundColor: 'purple',
  color: 'white',
  padding: '10px 20px',
  border: 'none',
  borderRadius: '5px',
  cursor: 'pointer',
};

function StyledButton() {
  return <button style={buttonStyle}>Click Me</button>;
}
Enter fullscreen mode Exit fullscreen mode

The button’s got attitude now, don’t you think? 😎

💥 External CSS (The Pro Approach)
Let’s take it up a notch and move the style to a separate CSS file. Why? Because we like to keep things clean. 💼

/* styles.css */
.btn {
  background-color: teal;
  color: white;
  padding: 10px 20px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}
Enter fullscreen mode Exit fullscreen mode

Now we’re talking:

import './styles.css';

function StyledButton() {
  return <button className="btn">Click Me</button>;
}
Enter fullscreen mode Exit fullscreen mode

3️⃣ Click Events: Interactivity Unlocked 🎉

Now here comes the fun part: Click Events! Time to make things move and groove. 😜

Here’s a little counter button, because... who doesn't love clicking things?

import React, { useState } from 'react';

function ClickHandler() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times! 🚀</p>
      <button onClick={() => setCount(count + 1)}>Click Me</button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

Click it—watch the magic happen. You’re basically a magician at this point. 🎩✨

4️⃣ Mapping Data: Efficiency Meets Elegance 🏆

If you’ve got a list, why not map it to the screen? 🖥️ It’s faster than your favorite pizza delivery.

const items = ['React', 'JavaScript', 'CSS'];

function ItemList() {
  return (
    <ul>
      {items.map((item, index) => (
        <li key={index}>{item}</li>
      ))}
    </ul>
  );
}
Enter fullscreen mode Exit fullscreen mode

No more boring loops—just elegant, dynamic rendering. 😎

5️⃣ Conditional Rendering: Dynamic Displays 🎭

Sometimes your app needs to think. Is the user logged in? Or not? 🤔 We’re about to make your app smart:

import React, { useState } from 'react';

function ConditionalRendering() {
  const [isLoggedIn, setIsLoggedIn] = useState(false);

  return (
    <div>
      {isLoggedIn ? (
        <h1>Welcome back, User! 🎉</h1>
      ) : (
        <h1>Please log in to continue. 😢</h1>
      )}
      <button onClick={() => setIsLoggedIn(!isLoggedIn)}>
        {isLoggedIn ? 'Log Out' : 'Log In'}
      </button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

It’s like a little emotional rollercoaster for your users. 😜

🛠️ Why These Features Matter (In Case You Haven't Noticed)

  • Styling & Events: Transform your app from meh to wow in seconds.
  • Conditional Rendering: Makes your app feel like it knows what’s going on. It’s like the app has mood swings... but in a good way.

🎉 And That’s a Wrap for Day 2! 🏁

We’ve officially touched on click events, styling, and conditional rendering. These are the building blocks of your next interactive web app, so get ready to show off your skills! 💪

What’s Next? 👀

In Day 3, we’re going deeper into state management and hooks. Trust me, things are about to get even more exciting! 🔥

💬 What do you think about these features? Drop your thoughts, questions, or random React jokes in the comments—I’m all ears! 🎧

Top comments (0)