ReasonJun

React : SPA 본문

Frontend/React

React : SPA

ReasonJun 2023. 6. 11. 02:39
728x90

SPA stands for Single-Page Application, and it is a web application architecture that aims to provide a seamless user experience by loading and rendering all necessary resources on a single web page, instead of navigating to multiple pages. React is well-suited for building SPAs due to its component-based architecture and efficient rendering capabilities.

 

In a React SPA, the initial HTML page is typically just a shell that includes a single <div> element, often referred to as the "root" element. The React application takes control of this element and dynamically renders the UI based on the application state.

 

Here are some key aspects of building SPAs in React:

  1. Routing: React Router is a popular library used for handling client-side routing in React applications. It allows you to define routes and map them to specific components, enabling navigation between different views without full page reloads. React Router provides components such as <BrowserRouter>, <Switch>, and <Route> to manage routing in a declarative manner.
  2. Component-Based Architecture: React's component-based architecture fits well with the modular nature of SPAs. You can create reusable and composable components that encapsulate their own logic and UI. These components can be combined to build complex UI structures and handle user interactions.
  3. State Management: React provides local component state using the setState method or the useState hook. However, for more complex applications, you might need a centralized state management solution like Redux or MobX. These libraries allow you to manage application-wide state, share data between components, and handle complex state transitions.
  4. AJAX and API Integration: SPAs often communicate with servers through AJAX requests to fetch data or perform actions. React provides the fetch API or libraries like Axios to make HTTP requests. You can perform data fetching and update the component state accordingly, triggering re-renders to display the fetched data.
  5. Optimizing Performance: SPAs can potentially load a large amount of JavaScript code, which may impact performance. React offers optimization techniques such as lazy loading, code splitting, and using React Suspense to improve performance. These techniques allow you to load only the necessary components and resources when needed, reducing the initial load time.

By leveraging React's powerful features and ecosystem, you can build robust and interactive SPAs that provide a seamless user experience, smooth navigation, and efficient rendering of UI components.

 

Principle of screen change in SPA

In a SPA that only has index.html, it is created using the HTML5 History API.

Using the history api in the javascript area, it makes it as if the screen movement has occurred within the current page.

728x90

'Frontend > React' 카테고리의 다른 글

React : Passing values to components through props  (0) 2023.06.11
React : Pros and Cons of SPA  (2) 2023.06.11
React : lifecycle  (0) 2023.06.11
React : React.Component / Component (class)  (0) 2023.06.11
React : state / props  (0) 2023.06.11
Comments