Photo by Lautaro Andreani on Unsplash
7 Topics to Know in React To Get Started.
There some topics that are basics in react that can provide the learner a strong foundation in exploring the technology further. Discover those here.
Introduction
React is a gamechanger for web developers. It's a special toolbox in JavaScript that helps create amazing things on websites. Imagine it as a set of building blocks—small, smart, and easy-to-use pieces—that can be combined to make anything from a simple button to a whole social media site.
It's very popular among developers because it's friendly and has a big community who are always ready to help. React helps build websites faster and keeps them organized. You can reuse your code, so it's like building blocks—you use the same pieces again and again to make different things. Plus, React works for all kinds of projects, whether it's a small blog or a big app like Facebook.
1. Components
In React, components are essential for a website—they're the special parts that make up everything you see and interact with. Think of them as small, smart building blocks that you can combine to create amazing web pages. Each component is like a mini-app, with its own job and superpowers.
For example, imagine you're making a website. You might have a "header" component for the top part with the logo and navigation, a "card" component for each post or item, and a "footer" component for the bottom part with extra info. These components can be reused everywhere in your website, just like using the same favorite toys to build different things each time.
What's really awesome about components is that they're super easy to manage. You can change one component without affecting the others, just like how fixing one toy block doesn’t break the whole Lego creation.
The following code snippet is how you create a component in React.
import React from 'react';
const Welcome = () => {
return (
<div>
<h1>Hello World!</h1>
</div>
);
};
export default Welcome;
To see the component in action, you need to use this component in the App.js file. The example is given below.
import React from 'react';
import Welcome from './Welcome';
const App = () => {
return (
<div>
<Welcome/>
</div>
);
};
2. Magic of JSX
JSX is like a special language that React superheroes use to describe how their components should look. It's a mix of JavaScript and HTML, kind of like putting puzzle pieces together to build a cool picture.
Instead of writing separate functions or methods to create each piece of a web page, JSX lets you write everything in one place, making it easier to see and understand how things fit together. For instance, you can write HTML-like code directly inside your JavaScript files. So, if you want to create a button, you just write something that looks like HTML inside your JavaScript code.
The cool thing is, JSX helps React understand what you want to show on the screen. When React sees this special code, it translates it into regular JavaScript that creates the actual components you want. It's like giving React a special set of instructions written in a language that's easy for both developers and React to understand. This way, you can build awesome web pages by describing how things should look and work in a way that's familiar and straightforward.
import React from 'react';
const Greeting= () => {
const greeting = 'Hello!';
return (
<div>
<h1>{greeting}</h1>
</div>
);
};
export default Greeting;
3. Displaying Data
Imagine having a box of toys; each toy is a piece of data, and you want to showcase them on your website.
You use special places called components to display this data. Let's say you have a list of superheroes' names, like Spider-Man, Batman, and Wonder Woman. You'd create a component that knows how to show these names nicely on your web page. It's like giving each toy its own stage to shine.
React lets you easily connect these components to your data. So, when you want to display those superhero names, you give React the list and tell it which component to use. React takes care of arranging everything on your webpage, making sure each piece of data appears where and how you want it.
As you can see from the code below, you can use curly braces({}) to use a JavaScript variable inside a JSX code. You can use the dot operator to specifically access a particular information (which is similar to working with objects in JavaScript.
import React from 'react';
const UserData = () => {
const user = {
name: 'John Doe',
age: 30,
};
return (
<div>
<p>Name: {user.name}</p>
<p>Age: {user.age}</p>
</div>
);
};
export default UserData;
4. Style your Webpage
One way is to use regular CSS (Cascading Style Sheets) to make things pretty. Just like how you pick different clothes and accessories to create a unique look, CSS helps you choose colors, sizes, and layouts for your website elements. You can write CSS in a separate file or even directly inside your React components to style them however you want.
Another cool trick in React is using special libraries like Styled Components. It's like having a fashion designer for your website! With Styled Components, you write your styles right alongside your React components. It's like saying, "Hey, this button should be blue, and it should have a nice shadow," all in the same place where you create the button itself.
There are other ways to style your JSX elements. But this is the easiest way to style your website.
import React from 'react';
const StyleComponent = () => {
const box = {
backgroundColor: 'lightblue',
borderRadius: '5px',
};
return (
<div style={box}>
<h1>Hello, Styling!</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliq.</p>
</div>
);
};
export default StyleComponent;
5. Using props
Props are like the messages that one superhero (parent component) sends to another superhero (child component) to tell them what to do or how to look.
Imagine you have a "Card" component that shows information about superheroes. The props are the messages you send to this "Card" component, like their names, powers, or even their colors. These messages help the "Card" component know what specific superhero information to display and how to show it.
What's great about props is that they make components flexible and reusable. You can send different messages (props) to the same "Card" component to show different superheroes. Just like changing the message in a secret code, props let you change how components behave or look without having to rewrite them every time.
The below code is an example for the parent component that passes in the data.
import React from 'react';
import ChildComponent from './ChildComponent';
const ParentComponent = () => {
const greeting = 'Hello from Parent!';
return (
<div>
<h1>Parent Component</h1>
<ChildComponent message={greeting} />
</div>
);
};
export default ParentComponent;
The below code refers to the working of props in the child component by displaying the information from the parent component.
import React from 'react';
const ChildComponent = (props) => {
return (
<div>
<h2>Child Component</h2>
<p>{props.message}</p>
</div>
);
};
export default ChildComponent;
6. Mapping Components
Using mapping, you can loop through this list of superheroes and, for each hero, create a card component that shows their details. It's like having a superhero factory that produces cards for each hero automatically! You pass the superhero's information as props to the card component, and it uses that information to create a unique card for each hero in your list.
This technique makes your code super efficient and clean. Instead of repeating the same code for each superhero, you write it once and let the mapping magic do the rest.
In the below code, Array.map
()
function to iterate over each item in the items
array.
import React from 'react';
import ListItem from './ListItem';
const List = () => {
const items = ['Apple', 'Banana', 'Orange', 'Grapes'];
return (
<div>
<h2>Fruit List</h2>
<ul>
{items.map((item, index) => (
<li>{item}</li>
))}
</ul>
</div>
);
};
export default List;
7. Hooks
In React, hooks are like special tools that give your functional components superpowers—allowing them to do complex things without becoming too tangled up. These hooks are functions that let you use state, effects, and other React features in functional components, which were traditionally available only in class components.
One of the most commonly used hooks is the useState
hook. It's like having a magic box within your functional component where you can store and manage changing information. For instance, if you're building a counter, useState
helps you keep track of the count without needing a class component. It's simple and powerful!
Then there's the useEffect
hook, which is like a wizard that performs actions whenever something happens—like when your component shows up or when a specific piece of data changes. This hook allows you to perform side effects in your functional components, such as fetching data from an API or updating the title of the webpage.
Hooks, in essence, empower functional components to be as capable as class components, making it easier to manage state, perform actions, and build dynamic and interactive interfaces—all within a cleaner and more straightforward functional approach. They're the secret sauce that makes functional components so powerful in React.
Conclusion
By grasping these foundational elements, you're equipped to architect captivating applications, unleash creativity through code, and embark on an ever-evolving journey of growth within the React ecosystem.
Take these topics as your guiding stars—they'll illuminate your path as you navigate the world of React. Embrace the learning process, experiment fearlessly, and let these foundational concepts be your launchpad into a rewarding and fulfilling journey of React development. The adventure awaits, and with these key topics as your compass, you're ready to set sail into the boundless possibilities of React!