DEV Community

NathanWelliver
NathanWelliver

Posted on

My intro to React

Introduction
In the second phase of my journey at Flatiron School, I dove deep into the world of React, a powerful JavaScript library for building user interfaces. This phase was packed with learning experiences, from understanding components and state management to handling side effects and client-side routing. In this blog post, I'll share some of the key technical aspects I learned, the challenges I faced, and the rewarding moments that made this phase truly impactful.

Understanding Components and State in React
One of the core concepts in React is the idea of components. Components allow you to break down your UI into reusable pieces, making your code more modular and maintainable. Alongside components, state management plays a crucial role.

Setting Up State
Before setting up state in a component, it's important to determine whether the component actually needs state. If it does, you can set it up using the 'useState' hook. Here's a simple example:

import { useState } from "react";

const [allPies, setAllPies] = useState([]);
Enter fullscreen mode Exit fullscreen mode

In this snippet, 'allPies' is the state variable, and 'setAllPies' is the function used to update the state. This setup allows you to manage the state within your component efficiently.

Managing State with Functions and Effects
You can set the state either through a function or using the 'useEffect' hook. The 'useEffect' hook is particularly useful for handling side effects, such as data fetching.

Here's an example of both approaches:

import { useEffect, useState } from "react";

const MyComponent = () => {
  const [allPies, setAllPies] = useState([]);

  useEffect(() => {
    fetch("http://localhost:3001/pizzas")
      .then(response => response.json())
      .then(data => setAllPies(data))
      .catch(error => console.log(error));
  }, []);

  const handleClick = (pizza) => {
    setEditPie(pizza);
  };

  return (
    <div>
      {/* Render your component UI here */}
    </div>
  );
};
Enter fullscreen mode Exit fullscreen mode

Overcoming Challenges with Side Effects
Understanding side effects and using the same child component for different parent components were some of the toughest challenges I faced. The concept of side effects, especially, took a while to grasp. Initially, I struggled to understand when and how to use the useEffect hook. Through numerous discussions with Peer Technical Coaches (PTCs) and a lot of trial and error, I finally understood how to manage side effects and reuse components effectively.

One practical example was fetching data from an API. I needed to ensure that the data was fetched every time the component was mounted or when specific dependencies changed. The 'useEffect' hook helped manage this efficiently, as shown in the earlier code snippet. Another challenge was reusing a child component in different parent components without breaking the state or functionality. This required a solid understanding of props and how to pass data effectively between components.

Building My Project: A Rewarding Experience
The most rewarding part of this phase was building my own website for the project. I was amazed at how quickly I could put together the functionalities I needed. In less than a day, I had most of my project completed. This experience not only boosted my confidence but also reinforced the skills I had learned throughout the phase.

Creating a dynamic and responsive website required me to apply everything I had learned about React. I had to think critically about how to structure my components, manage state, and handle user interactions. For instance, implementing client-side routing with React Router allowed me to create a seamless navigation experience for users without reloading the entire page. Here's a basic example of setting up React Router:

import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';

const App = () => (
  <Router>
    <Switch>
      <Route path="/" exact component={HomePage} />
      <Route path="/about" component={AboutPage} />
      <Route path="/contact" component={ContactPage} />
    </Switch>
  </Router>
);

const HomePage = () => <div>Home</div>;
const AboutPage = () => <div>About</div>;
const ContactPage = () => <div>Contact</div>;

export default App;
Enter fullscreen mode Exit fullscreen mode

Key Takeaways
Start Your Projects: No matter how daunting a project or task may seem, the hardest part is getting started. Once you begin, you'll often find it easier to make progress than you anticipated.

State Management: Properly managing state is crucial for creating dynamic and interactive UIs. Understanding when and how to use state can greatly enhance your development process.

Side Effects: Handling side effects, such as data fetching, requires a good grasp of the 'useEffect' hook. This knowledge is essential for working with external data sources in React.

Component Reusability: Learning to reuse components across different parts of your application can save you time and effort, making your codebase more efficient and maintainable.

Conclusion
Phase 2 at Flatiron School has been a transformative experience. From grappling with complex concepts to building a functional website, I've grown significantly as a web developer. The skills and knowledge I've gained will undoubtedly help me in my future career. If there's one piece of advice I'd give to fellow learners, it's to start on your projects—because the journey of a thousand miles begins with a single step.

Top comments (0)