Hands-on Practice
Last updated
Last updated
"Thinking in React" is a mindset and a process for approaching the development of user interfaces with the React library. It's about breaking down complex UIs into smaller, manageable, reusable components and then defining how data flows between them. This approach makes building and maintaining complex applications much easier.
Break the UI into a component hierarchy: Start by drawing boxes around every component (and sub-components) in your UI design. Give them descriptive names. This is your component tree.
Single Responsibility Principle: Each component should ideally do one thing and do it well. If a component grows too complex, break it down further.
Build a static version in React: Focus on creating a working version of your UI that renders the data correctly. Don't worry about interactivity yet. This step ensures you have the correct component structure and data flow.
Props: Use props to pass data from parent components down to child components.
Identify the minimal (but complete) representation of UI state: Determine the absolute minimum set of state variables your app needs. Avoid redundant state.
State: State is data that changes over time and affects the UI. Identify what data needs to change in response to user interactions or other events.
Identify where your state should live: Determine which component should "own" each piece of state. This is often the common ancestor of all components that need to use or modify that state.
Lifting State Up: If multiple components need access to the same state, lift the state up to their nearest common ancestor component.
Add inverse data flow: Implement the interactivity. This usually involves passing callback functions down through props, allowing child components to update the state in their parent components.
Event Handlers: Use event handlers (e.g., onClick
, onChange
) to trigger state updates.
Set up your development environment:
Node.js and npm (or yarn): Install Node.js, which includes npm (Node Package Manager). Yarn is an alternative package manager you can choose.
Create Vite+React App : This is the easiest way to start a new React project:
Understand basic JavaScript (ES6+): React relies heavily on modern JavaScript features like:
const and let: For variable declarations.
Arrow functions: Concise function syntax.
Destructuring: Extracting values from objects and arrays.
Modules (import/export): Organizing code into reusable modules.
JSX: A syntax extension that allows you to write HTML-like code within JavaScript.
Learn the core React concepts:
Components: The building blocks of React UIs.
JSX: How to write HTML-like code in JavaScript.
Props: Passing data from parent components to child components.
State: Managing data within a component that can change over time.
Hooks: Functions that let you "hook into" React state and lifecycle features from functional components. Start with useState
and useEffect
.
Events: Handling user interactions (e.g., clicks, form submissions).
Practice and iterate: The best way to learn React is by doing. Build projects, experiment, and don't be afraid to make mistakes. Learn from them and keep practicing.
Component Hierarchy: One component: Counter
.
Static Version: Render a number (initially 0) and two buttons ("Increment" and "Decrement").
Minimal State: One piece of state: count
(initially 0).
State Location: The Counter
component owns the count
state.
Inverse Data Flow: Add onClick
handlers to the buttons that update the count
state using setState
.
This guide provides step-by-step instructions to create a React Calculator App with functionalities to Add, Subtract, Multiply, and Divide two numbers. The project will include two main files: Calculator.jsx
and Result.jsx
.
check whether you already installed a right version of node.js
Open Bash/PowerShell terminal and create your working directory, ie. "react-sandbox". Change the directory to "react-sandbox" and install vite latest version.
Then follow the prompts!
Project name: react-handson-practices
Select a framework: > React
Select a variant: > JavaScript
Change the directory to "react-handson-practices". And then install and run.
Install any necessary dependencies (optional): While this basic app doesn't require additional libraries, you can install styling libraries like Bootstrap if needed:
Click : http://localhost:5173
Navigate to the src
directory and create a new folder named components
.
Inside the components
folder, create two files:
Calculator.jsx
Result.jsx
Think in React way - Layout Design(Paper work)
Result.jsx using arrow function
Calculator.jsx
App.jsx
Replace the contents of App.jsx
with the following:
Run the app:
Open your browser and navigate to http://localhost:5173
.
Result Component: Displays the calculated result.
Calculator Component: Handles user input, operator selection, and calculation logic.
go to vite.dev site and click get started ().
Vite requires version 18+ or 20+.