React Fragments enables the bundling of multiple child elements without the need for additional nodes in the DOM.
For this purpose use <React.Fragment> or its shorthand syntax <> ... </>
In above example, MyComponent returns two <div> elements wrapped in a React.Fragment.
The grouping does not add an extra layer to the DOM.
It keeps the structure clean and efficient.
Fragments can accept keys, which is useful when mapping a collection to an array of elements, making them quite handy.
This is a simpler and modern way to consume or use context
It is available in functional components.
It takes the context object itself as an argument and returns the current context value.
The Context API in React is a tool for:
handling global state
and transmitting data across the component hierarchy.
Nevertheless, it doesn't used for all scenarios of state management.
In React, boundaries refer to defining a separation or limits between different parts of your application's UI or logic.
Creating boundaries improve code organization, maintainability, and scalability.
Functional or component separation or decomposition: Break down the UI into smaller, reusable components(function) that each have a single responsibility.
This promotes code reuse and makes components easier to understand and maintain.
This is the most common type of boundary.
Container components are responsible for data fetching and state management.
Presentational components do focus on rendering UI.
This maintains a clear separation of data and presentation logic.
Local Component State:
Use local state within components for managing UI-specific state that doesn't need to be shared with other components.
Use global State Management with the use of state management libraries like Redux, Context API, or Recoil for managing
global application state that can be accessed across multiple components.
This centralizes state logic and avoid using prop.
Dynamic Imports: Employ dynamic import() to partition your code into smaller units that are fetched as needed.
This approach enhances the initial page loading speed by only loading the code essential for the present view.
React.lazy() and Suspense:
React.lazy() and Suspense are two features in React used to do code splitting and improving the performance of your application by loading components asynchronously .
React.lazy() is a function that dynamically import a React component(loads only components that are needed to load th app).
It significantly improve the initial loading time of the application(especially for large applications that has lots of components).
Think we have a component named MyComponent that you want to import lazily:
Import it lazily in another component as follows:
In the example above, we see Suspense wraps the lazy-loaded component <LazyLoadedComponent >.
The fallback prop specifies what should be rendered while the component is loading.
/p>
While the LazyLoadedComponent is being fetched and loaded asynchronously, We see a loading indicator instead of a blank screen, providing a better user experience.
Error Boundaries: Use error boundaries to handle errors (catch JavaScript errors) that occur in your application.
It prevents the application from crashing when an unexpected error occurs.
Error boundaries in React catch JavaScript errors occurring in their child component tree.
Instead of letting these errors disrupt the user experience, error boundaries log them and display a backup UI.
These boundaries capture errors during rendering, lifecycle methods, and constructors of all components below them.
Context Providers:
Use Context API to pass data or functions down through the component tree without manually passing props at each level.
Use or define context providers at appropriate boundaries to encapsulate related state or behavior.
API Endpoints:
Create or define clear boundaries between client-side and server-side logic.
Use API endpoints to communicate between the frontend and backend parts of your application, ensuring separation of concerns and security.
We define the withLogging HOC, which takes a WrappedComponent as an argument.
Inside the HOC, we return a new component that renders the WrappedComponent and logs its props before rendering.
We then wrap our MyComponent with the withLogging HOC to create the EnhancedComponent.
When EnhancedComponent is rendered, it logs the props before rendering MyComponent.
Use the above (example 6) to enhance any component
HOCs introduce an additional layer of component abstraction, which can sometimes make the component hierarchy more challenging to understand.
HOCs do not modify the original component. Instead, they create a new component with enhanced functionality, which may affect component identity and lifecycle methods.
HOCs are a powerful tool, but overuse can lead to code complexity and reduced readability. Use them judiciously and consider alternative patterns such as Render Props or Hooks when appropriate.
Overall, Higher-Order Components are a valuable tool in the React developer's toolkit for enhancing component functionality, promoting code reuse, and improving application architecture.
HOCs are like kind of wrappers for the components to add additional functionalities (like checking for login, handling errors, loading data) without making changes to the original components. It is helpful in reusing code and keeping components organized.
It is maintained by Facebook, stands as a JavaScript testing framework, easy to use with rapidity, and robust capabilities. It offers features like snapshot testing and mocking.
React Testing Library, a testing utility designed for React.
It mirrors user interaction with the application.
It provides a user-friendly and simple API for querying and interacting with components.
React Testing Library is a powerful tool for testing React components.
It tests that components behave correctly.
React Testing Library integrates with popular testing frameworks like Jest.
We can write and run tests within our existing testing setup.
For integration testing, we use tools like Cypress or testing library's testing frameworks.
In React, there are several ways to style components. Here are some common methods to style the React App.
W can apply styles directly within the JSX using JavaScript objects.
We can create separate CSS files and import them into your components.
The Fetch API comes integrated into contemporary browsers, offering a straightforward and robust mechanism for asynchronously retrieving resources or data.
{JSON.stringify(data, null, 2)}}
A popular HTTP client library to make AJAX requests in JavaScript.
{JSON.stringify(data, null, 2)}}
Use of the useEffect hook in combination with the Fetch API for data fetching.
{JSON.stringify(data, null, 2)}}