These excerpts detail React development concepts and practices through hands-on challenges. The text introduces core concepts like components, JSX syntax, and rendering. It explains props and state for building reusable, data-driven UIs, including handling complex data like arrays and objects. Various challenges focus on practical skills like styling components, managing events, and fetching data from external APIs. The final segments involve Capstone projects designed to reinforce all concepts, including game building as a method to solidify React skills. The overarching theme is learning React through active participation and iterative problem-solving.
React Fundamentals Study Guide
Quiz
1. How do you typically select the DOM node where React will insert its content?
You can select a DOM node using standard JavaScript DOM selection methods such as document.getElementById() or document.querySelector(). The selected node serves as the root for the React application.
2. What does the createRoot method do, and where does it come from?
The createRoot method creates a root instance that allows React to manage and render content within a specific DOM node. It is imported from react-dom/client.
3. What does the render method do?
The render method takes React components or elements (JSX) and displays them inside the specified root DOM node, managed by createRoot.
4. What is JSX?
JSX is a syntax extension to JavaScript that allows writing HTML-like structures within JavaScript code. It is transformed into regular JavaScript function calls by tools like Babel.
5. Why is it important to export components from their respective files?
Exporting a component allows it to be used in other files within the application. This promotes modularity and code reusability. It can be accomplished using export default or named exports.
6. What is the difference between a default export and a named export?
A default export allows you to import a module under any name, while a named export requires you to import a module using its specific name.
7. What is the purpose of using a build tool like Vite?
Build tools like Vite streamline the development process by providing features like fast development servers, module bundling, and optimization for production. They handle the complexities of modern JavaScript development, such as JSX transpilation and dependency management.
8. What is the purpose of React fragments, and what do they look like?
React fragments allow you to group a list of children without adding extra nodes to the DOM. They are represented by empty tags <></> or the <React.Fragment> component.
9. What are props in React?
Props (short for properties) are a mechanism for passing data from a parent component to a child component. They enable components to be dynamic and reusable.
10. How do you pass a value to a component that is not a string?
When passing non-string values via props, enclose the value in curly braces. For example: `<Component upvotes={10} isPun={true} />`.
Essay Questions
- Discuss the benefits of using components to build user interfaces, and explain how React facilitates component-based development.
- Explain how JSX simplifies the process of writing React user interfaces compared to using vanilla JavaScript. Provide examples to illustrate your points.
- Describe the steps involved in setting up a React development environment using Vite. Explain the role of Node.js and npm in this process.
- Explain the difference between state and props in React, and why both are important for creating dynamic user interfaces. Provide examples.
- Discuss the importance of accessibility in web development, and describe some of the techniques that can be used to make React applications more accessible.
Glossary
- DOM (Document Object Model): A programming interface for HTML and XML documents. It represents the page so that programs can change the document structure, style, and content. React uses a virtual DOM to efficiently update the actual DOM.
- Component: A self-contained, reusable piece of code that represents a part of the user interface. React applications are built by composing components together.
- JSX: A syntax extension to JavaScript that allows you to write HTML-like code in your JavaScript files.
- createRoot: A method in React that creates a root object for rendering a React component into a specific DOM node.
- render: A method used to display React elements or components within a specified DOM node.
- Module Bundler: A tool that takes JavaScript and its dependencies and packages them into a single file or a set of files for use in a browser.
- Vite: A fast build tool that serves your code during development and bundles it for production.
- Node.js: A JavaScript runtime built on Chrome’s V8 JavaScript engine, which allows you to run JavaScript on the server-side.
- npm (Node Package Manager): A package manager for JavaScript that allows you to easily install and manage dependencies in your projects.
- Props (Properties): Data passed from a parent component to a child component. Props are read-only from the child component’s perspective.
- State: Data that is managed within a component. Unlike props, state can be changed by the component itself, triggering a re-render of the component and its children.
- Event Handler: A function that is called when a specific event occurs (e.g., a button click).
- Conditional Rendering: A technique used in React to display different content based on certain conditions.
- Accessibility (a11y): The practice of designing and developing websites that are usable by people with disabilities.
- aria-label: An HTML attribute used to provide a descriptive label for an element, especially for assistive technologies like screen readers.
- clsx: A tiny utility for constructing className strings conditionally and concisely.
- key: A special string attribute you need to include when creating lists of elements. Keys give the elements a stable identity.
- useEffect: A React Hook that lets you perform side effects in function components. Data fetching, setting up subscriptions, and manually changing the DOM in React components are all examples of side effects.
- Side Effect: Operations that affect something outside the scope of the current function being executed, such as directly manipulating the DOM, or fetching data from an API.
- Spread syntax: A modern javascript feature that allows you to copy all or part of an existing array or object into another array or object.
- Ternary Operator: A Javascript operator that is a shorthand way to write an if…else statement in one line. condition ? expressionIfTrue : expressionIfFalse
React Fundamentals: A Practical Introduction
Okay, here’s a briefing document summarizing the main themes and ideas from the provided source:
Briefing Document: React Fundamentals and Setup
Overview:
This document summarizes a lesson focused on introducing the fundamentals of React development, including rendering content to the DOM, working with JSX, setting up a local React development environment using Vite, component creation, styling, props, state and conditional rendering. The lesson employs a hands-on approach, encouraging the learner to actively rewrite and modify code through challenges.
Main Themes and Ideas:
- Rendering with React:
- React uses a “root” to insert its content into the DOM. This is achieved using createRoot from react-dom/client.
- The render method of the root is then used to display content, which often looks like HTML within JavaScript (JSX).
- Quote: ” …up is going to be like the root of our application it’s the one place where react will insert everything so what I need to pass to the create root method is this Dom node and I can get it by doing any regular Dom node selection I can say document. getet element by ID for example and pass in the ID of root and now that I have a so-called root I can call a method on that route called render…“
- Quote: “…I’m going to pass what looks an awful lot like HTML inside of my JavaScript…”
- JSX Syntax:
- JSX allows writing HTML-like structures within JavaScript code.
- It is crucial to use the .jsx extension for files containing JSX so that Vite can compile it correctly.
- JSX allows developers to lean on a familiar HTML syntax.
- Quote: “…we’re starting to see one of the benefits of using react is we can lean on a familiar syntax in HTML with a couple tweaks here and there that we’ll be discovering as we go through this course…“
- Setting Up a Local React Environment with Vite:
- Vite is recommended as a fast build tool for React projects.
- It requires Node.js and npm to be installed. NVM (Node Version Manager) is suggested for easy installation and updates.
- The command npm create vite@latest is used to scaffold a new React project.
- Quote: “…We’re going to be using the recommended build tool called vit and yes it is pronounced vit that’s French for quick or fast…“
- Components:
- React applications are built using components.
- Components can be moved into their own files for better organization and reusability.
- JSX elements need to be enclosed by a single top level or parent element.
- Components are exported and imported using export default and import. When using export default, you do not need to surround the component name when importing.
- Quote: “…moving these components into their own files is a really simple task…“
- Styling:
- The lesson encourages practicing CSS styling to customize the appearance of React applications.
- Opportunities are given to put design tweaks on the projects and post them in the Scrimba Discord community.
- Styling can be controlled with CSS files, including font families, padding, colors and flexbox properties.
- Quote: “…I want this to be not only an opportunity to practice playing in the code which is probably why you’re here on scrimba in the first place but also to give you an opportunity to interact with the scrimba community…“
- Props
- Props are a mechanism for passing data from a parent component to a child component.
- Props use a similar concept to attributes in HTML.
- Props can be of any JavaScript data type, not just strings.
- JSX allows switching into Javascript mode by using curly braces, and in this mode developers can pass props that aren’t of the string data type.
- Props can have default values, using props.propertyName || defaultValue if a certain condition exists, react gives the developer the control to conditionally render things on the page.
- State
- useState is a React hook that allows components to manage and update their own data.
- The useState hook returns an array with two elements: the current state value and a function to update it (the “setter” function).
- There are two options for what you can pass into the State Setter function. One, is the new version of state. And two, is a callback function.
- The callback function receives the old version of state as a parameter.
- The useState hook allows developers to make the page dynamic.
- Conditional Rendering
- Conditional rendering enables displaying different content based on specific conditions.
- Different ways to apply conditional rendering are: if/else statements, the ternary operator, and the && (AND) operator.
- The ternary operator can be used in JSX, allowing you to write concise conditional logic inline.
- React using JavaScript under the hood allows developers to use native Javascript APIs and techniques to help display things on the page.
- Side Effects and useEffect
- The useEffect hook is used to perform side effects in functional components (e.g., interacting with the browser API, fetching data).
- It’s important to clean up side effects when a component unmounts to avoid memory leaks and unexpected behavior.
- Returning a function from the useEffect callback allows you to specify cleanup logic.
- Focus on Practice and Learning by Doing:
- The lesson emphasizes the importance of actively rewriting code and completing challenges to reinforce learning.
- Taking quizzes like the one included is scientifically proven to improve the developer’s ability to retain information and recall it at a later time.
Key Quotes Emphasizing Active Learning:
- “I really want you to try this challenge to the best of your abilities before you just simply click the button and move on you’ll hear me say it a million times but you really will just be shorting your own education and your own practice if you decide to take the easy way out…“
- “…doing that Discovery process is going to be much better for your long-term memory and your skill building in writing react code then it will be to just skip this Challenge and move forward…“
- “…get that muscle memory in get the repetitions in and remember to take breaks along the way throughout this course…“
In Summary:
The lesson provides a practical introduction to React development, covering essential concepts and techniques. It places a strong emphasis on active learning through coding challenges and encourages the learner to experiment and explore further.
React Fundamentals: A Concise Overview
### What is React and how does it work at a high level?
React is a JavaScript library for building user interfaces. It allows developers to create dynamic and interactive UIs by breaking them down into reusable components. Under the hood, React manipulates the DOM (Document Object Model) efficiently to update the view when data changes.
### What is `createRoot` and what does the `render` method do?
`createRoot` is a function from the `react-dom/client` library that creates a root for your React application. You pass a DOM node (usually an element with an ID like “root”) to `createRoot`, indicating where React should insert its content. The `render` method, called on the root, then takes a React component and renders it into that DOM node, effectively displaying the UI defined by that component.
### How do you write what looks like HTML inside of JavaScript in React?
React uses JSX (JavaScript XML), a syntax extension to JavaScript. JSX allows you to write HTML-like code within your JavaScript files. This code is then transformed into regular JavaScript function calls by a tool like Babel, ultimately creating the DOM elements that make up your UI.
### How can you set up a React project locally on your machine?
The recommended tool for setting up a React project locally is Vite. You need to have Node.js and npm (Node Package Manager) installed first. Then, you can run the command `npm create vite@latest` in your terminal. This will guide you through a wizard to create a new project, select React as the framework, and choose a variant (like JavaScript). After the project is created, you `cd` into the project directory, run `npm install` to install dependencies, and then `npm run dev` to start a local development server.
### How do you separate parts of your app into different components?
To create a React component, you typically create a new `.jsx` file. You then define a JavaScript function that returns JSX, representing the UI for that component. To use the component in another file (like `index.jsx`), you need to `export` it from its file (usually using `export default`) and `import` it into the other file.
### How can you add styling to your components?
You can style your components using CSS. You can either link an external CSS file or use inline styles. Class names can be applied to JSX elements to connect them to CSS rules defined in your stylesheet.
### What are props and how do you use them to pass data between components?
Props are a way to pass data from a parent component to a child component. They are similar to HTML attributes. You pass props to a component when you use it in JSX, like `<MyComponent name=”John” age={30} />`. Inside the `MyComponent` function, you access these props through the `props` object (e.g., `props.name`, `props.age`). Prop values can be strings, numbers, arrays, objects, or even functions.
### What is state in React and how do you update it?
State is a way for React components to manage and store data that can change over time. You initialize state using the `useState` hook, which returns two values: the current state and a function to update that state (the “state setter”). When you update the state using the state setter, React re-renders the component, reflecting the changes in the UI. When updating state that depends on the previous state value, use the callback function version of the state setter.
React Components: Structure and Functionality
React components are fundamental building blocks for creating user interfaces. They are reusable and composable pieces of code.
Here’s a breakdown of key aspects:
- Composability and Reusability: Custom components can encapsulate code, allowing you to render them multiple times throughout a document. For example, a custom component called my awesome navbar can include all the code for a navigation bar, and each time you render this component, you’ll get a copy of the navigation bar. If a change is made to the original component, it is reflected everywhere that component is used.
- JSX Syntax: React utilizes JSX, which looks like HTML within JavaScript, to describe the user interface. Although it looks like HTML, it is actually syntactic sugar on top of react.createElement and returns JavaScript objects. These objects describe what React should place on the page.
- Pascal Case: Custom components in React must be defined using Pascal case (e.g., TemporaryName). When rendering a custom component, it is typically enclosed in angle brackets, similar to HTML elements (e.g. <TemporaryName />).
- Declarative Nature: React is declarative, meaning you describe what should be done, and React handles the how. This contrasts with imperative programming, where you need to specify every step.
- React Elements: React components are functions that return React elements. React elements can be thought of as the React version of HTML elements. JSX syntax is converted into calls to react.createElement, which then turns them into JavaScript objects. React interprets these JavaScript objects and turns them into real DOM (Document Object Model) nodes.
- Props: React components can receive information in the form of data, and use that to produce reusable components on the page. This is where the concept of “props” comes into play. Props are properties passed down into a component to configure it. Components are not allowed to modify props, as props are immutable or unchangeable.
- State: State refers to any values that are managed by the component itself. The user interface is a function of the state of your component. React will only run a component if the props that it’s receiving change or it has a state value that changes.
- Fragments: Fragments are a built-in component from React that allow you to group multiple elements without introducing an extra DOM node. They can be written as <Fragment></Fragment> or with a shorthand <></>.
- Side Effects: React components should avoid side effects, meaning they should not affect any outside system. An example of a side effect would be making a post request to add an item to a list in a database every time a component runs.
- Rendering: When React renders a component, it runs the function of that component. This involves executing the code within the component, such as setting up state, defining functions, and returning JSX.
React State Management: An Overview
State refers to any values that are managed by the component itself. The user interface is a function of the state of the component, and React will run a component if the props that it’s receiving change or it has a state value that changes. In React, you describe what should be done, and React handles the how. All that is required is to keep the data, or state, of the application up-to-date, and React will handle the rest by updating the view and changing parts of the DOM in reaction to the state change.
Key aspects of state management:
- State and Re-rendering A component’s primary job is to take input (props) and return what should be displayed on the screen. React re-renders a component when its state changes.
- Immutability It is a no-no in React to directly modify the state. Whenever there is a need to change the state, it will never be changed directly.
- useState Hook To set up a variable that React will place in the view and re-render whenever that state changes, it is necessary to pull in that function from React. A common way to do this is to import a destructured useState Hook from the React library. This function returns an array where the first value is undefined and the second value is a function.
- Updating State The function that is returned from react.useState, if called and provided with a new value, will re-render the page. This will successfully update the state and trigger React to re-render the page with the new state displayed. When setting state, it is possible to pass the new version of state to replace the old version. The other way is to pass a callback function to the setState function; this callback function will return what the new value of state should be and will receive the old version of state as a parameter.
There are tools such as Context, Redux, and Zoo that can help avoid having to pass props many levels down in an application.
React Event Handling: A Comprehensive Guide
Event handling is a crucial aspect of creating interactive web applications in React, allowing users to interact with elements on the screen.
Here’s a breakdown of event handling in React:
- Event Listeners: React implements event listeners through properties that are added to React elements.
- Naming Convention: Event handler names are camel-cased (e.g., onClick). The syntax is similar to how it is done in HTML.
- Function Expressions: Instead of setting the attribute equal to a string of a function, it can be set to a JavaScript expression that you want to run.
- Event Object: Event handler functions have access to the event object, which contains information about the event that was fired. The event object can be used to access data from the element that triggered the event.
Adding Event Listeners
- Event listeners can be added as properties to React elements.
- Instead of using addeventListener to select elements and add a listener, event listeners are added directly in the JSX.
- It’s possible to define a function outside of the element and then call it in the element. It’s also possible to define a function directly in the element.
Form Events
- Forms have their own internal state-holding ability.
- Forms can use the onSubmit event to handle form submissions.
- The event.preventDefault() method can be used to prevent the default form submission behavior, such as page refresh.
- Form data can be accessed using the FormData API.
Accessibility
- When disabling elements, it’s important to also set the aria-disabled property to improve accessibility for users with assistive technologies.
- For dynamically rendered content, use aria-live regions to announce updates to assistive technologies.
Controlled vs Uncontrolled Components
- A controlled component is one that React is in control of and does reflect the current value of state.
- An uncontrolled component is one that React is not in control of and doesn’t reflect the current value of state.
React Props: Component Data Passing
Props in React are a mechanism for passing data from parent components to child components, facilitating the creation of reusable and dynamic user interfaces. Props allow components to receive and utilize data, similar to how parameters are passed into a function.
Key aspects of props passing:
- Purpose of Props: Props enable the creation of reusable components by allowing them to receive data and customize their behavior. They are a primary means of achieving reusability in React components, making components more flexible and adaptable.
- Passing Props: Props are passed to components in a way that is similar to passing attributes to HTML elements. For example, if you have a component, you can pass a prop like this: <MyComponent title=”Hello” />.
- Custom Props: In React, custom props can be defined and passed to components. This differs from HTML elements, where the attributes are predefined by the HTML specification. With React, you have the flexibility to choose the names and types of props that your components accept.
- Receiving Props: Inside a component, props are received as an object. This object contains all the properties that were passed to the component. It is common to name this object props, but this can be named whatever you want. The data on the props object can be accessed with regular JavaScript using props.name, props.title, and so on.
- Data Types: Props can accept any JavaScript data type, including strings, numbers, arrays, objects, and even functions. This flexibility allows you to pass complex data structures to components and control their behavior in various ways.
- Immutability: Components are not allowed to modify props, as props are immutable or unchangeable.
- One-Way Data Flow: Data can only flow downwards, from a parent component down to its children.
- Object Destructuring: Object destructuring can be used to extract values from the props object directly within a component. Instead of accessing props using props.name, you can destructure the object to access the name directly const { name } = props;.
- Spread Syntax: The object spread notation can be used to pass all properties of an object as props to a component. For example, if you have an object called entry, you can pass all its properties as props to a component like this: <MyComponent {…entry} />. React will take all of the individual properties of the entry object and create a new prop that matches each of the properties of this entry object.
Props enable developers to create modular, reusable, and data-driven components, which are key to building complex and maintainable React applications.
Code Refactoring: Improving Software Structure and Maintainability
Code refactoring is the process of restructuring existing computer code without changing its external behavior. It is intended to improve the nonfunctional attributes of the software.
Here are key considerations for code refactoring, based on the sources:
- Reasons for Refactoring: One reason to refactor code is to clean it up and make it easier to reason about. Refactoring can reduce the amount of code in a component. After refactoring, code should be easier to maintain.
- Componentization: During refactoring, it may be helpful to move sections of code into their own components. This can make the code easier to reason about.
- State Considerations: Refactoring may require critical thinking about how to communicate between parent and child components. This includes deciding whether state needs to move down to a child component or live in the parent component.
- Single Direction of Data Flow: When refactoring, it is important to set up a React-like way where there is a single source of truth and data flows in one direction, being passed down through props to the components that need it.
- State Location: It is best practice to keep state as locally defined as it needs to be.
- Derived State: Take advantage of derived state (state that is computed or derived from existing state) to reduce the need for useState. Ask whether a value needs to be saved in state, or whether it can be derived, and avoid synchronizing data in a side effect using useEffect.
- Accessibility: While refactoring, keep accessibility in mind when choosing which elements to put on the page. Also, it is important to consider how React is dynamically rendering or removing things from the page, and how to cater the code to those needs.

By Amjad Izhar
Contact: amjad.izhar@gmail.com
https://amjadizhar.blog
Affiliate Disclosure: This blog may contain affiliate links, which means I may earn a small commission if you click on the link and make a purchase. This comes at no additional cost to you. I only recommend products or services that I believe will add value to my readers. Your support helps keep this blog running and allows me to continue providing you with quality content. Thank you for your support!

Leave a comment