DEV Community

Cover image for Important React Concepts
Sonay Kara
Sonay Kara

Posted on • Edited on

Important React Concepts

Important React Concepts


1- React Hooks

You can use useReducer to manage complex state structures, you can useEffect to React Hook that lets you synchronize a component with an external system. You can useCallback/ useMemo for performance optimization, useRef for DOM access, and create custom hooks.


2. Render Props Pattern

Another way of making components very reusable, is by using the render prop pattern. A render prop is a prop on a component, which value is a function that returns a JSX element. The component itself does not render anything besides the render prop. Instead, the component simply calls the render prop, instead of implementing its own rendering logic.


3. Suspense

Suspense lets you display a fallback until its children have finished loading.

Example :

<Suspense fallback={<Loading />}>
  <SomeComponent />
</Suspense>
Enter fullscreen mode Exit fullscreen mode

4. Error Boundary

Error boundary is a React-specific component that wraps around a component tree and prevents any errors within that component from spreading and causing an entire application to crash.

To use it, you must simply wrap the component tree you want to protect with an error boundary component. The error border will detect errors and show the fallback UI when they happen within the wrapped component tree.

Example :

class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }

  static getDerivedStateFromError(error) {
    // Update state so the next render will show the fallback UI.
    return { hasError: true };
  }

  componentDidCatch(error, info) {
    // Example "componentStack":
    //   in ComponentThatThrows (created by App)
    //   in ErrorBoundary (created by App)
    //   in div (created by App)
    //   in App
    logErrorToMyService(error, info.componentStack);
  }

  render() {
    if (this.state.hasError) {
      // You can render any custom fallback UI
      return this.props.fallback;
    }

    return this.props.children;
  }
}


<ErrorBoundary fallback={<p>Something went wrong</p>}>
  <Profile />
</ErrorBoundary>
Enter fullscreen mode Exit fullscreen mode

5. Passing Data with Context

Usually, you will pass information from a parent component to a child component via props. But passing props can become verbose and inconvenient if you have to pass them through many components in the middle, or if many components in your app need the same information. Context lets the parent component make some information available to any component in the tree below it—no matter how deep—without passing it explicitly through props.


6. State Management

State management is a crucial concept in React, most popular JavaScript libraries in the world for building dynamic user interfaces.
Manage application state using Redux.


7. Code Spliting

Bundling is great, but as your app grows, your bundle will grow too. Especially if you are including large third-party libraries. You need to keep an eye on the code you are including in your bundle so that you don’t accidentally make it so large that your app takes a long time to load.

To avoid winding up with a large bundle, it’s good to get ahead of the problem and start “splitting” your bundle. Code-Splitting is a feature supported by bundlers like Webpack, Rollup and Browserify (via factor-bundle) which can create multiple bundles that can be dynamically loaded at runtime.

Code-splitting your app can help you “lazy-load” just the things that are currently needed by the user, which can dramatically improve the performance of your app. While you haven’t reduced the overall amount of code in your app, you’ve avoided loading code that the user may never need, and reduced the amount of code needed during the initial load.


Conclusion

In this article, I wrote about advanced concepts in React. These advanced concepts improve performance and maintainability in your react applications. You can basically understand and use these concepts


If you like my articles, you can buy me a coffee :)
Image description

Top comments (8)

Collapse
 
pengeszikra profile image
Peter Vivo
  1. If don't need to heavy weight state management, take look a simply one: pnpm add jsdoc-duck 65 LOC, borrowing: just copy to code into your codbase, don't need use as dependency.
Collapse
 
bh4skar profile image
Bhaskar Prajapati

Keep up!

Collapse
 
boby900 profile image
Boby Tiwari

Great read

Collapse
 
sonaykara profile image
Sonay Kara

thank you !

Collapse
 
hujunjun profile image
胡胡军

cool man

Collapse
 
sonaykara profile image
Sonay Kara

thank you

Some comments may only be visible to logged-in visitors. Sign in to view all comments.