Building Size-Aware React Components – LogRocket

Building Size-Aware #ReactJS Components:  by @b_edelstein #JavaScript

  • When using an HTML5 Canvas that has to be re-drawn whenever its size changesWhen building a layout where the user can adjust the sizes of different panesWhen using elements or 3rd party libraries that require a known size like react-virtualized, fixed-data-table, etc.When rendering elements with absolute positioning that need their position adjusted when the container size changes.When animating an element’s size with CSS with additional logic that depends on this animated sizeSome of these behaviors may be achievable with CSS, or by managing window size with the onResize event, but in the interest of building React components that are maximally reusable, it makes sense to encapsulate this logic into the component itself.
  • This method is called whenever the component’s size or position changes, and can be used to trigger side effects or put the dimensions into state.This solves most of the earlier examples but there’s one issue — a chicken and egg problem.
  • As such, we don’t always know the dimensions in the render function, so it’s not entirely possible for the component to have render logic based on its dimensions.react-sizemeTo solve this problem, we turn to a library called react-sizeme.
  • This library is similar to react-measure but uses a clever solution to solve the aforementioned problem.react-sizeme does an initial, invisible render of the component to measure its size.
  • Usually this feels quite fast, but in practice, I’ve noticed a slight delay in detecting changes which can lead to a component feeling slow.In general, react-measure has less of a performance impact, so it should be used if possible, but in cases where a component’s initial render depends on its size, then react-sizeme is a good option.Resources

When building React apps, it is sometimes advantageous for components to have awareness of their size and position on screen. This is useful both for rendering content initially, but also for…

@HappyFunCorp: Building Size-Aware #ReactJS Components: by @b_edelstein #JavaScript

When building React apps, it is sometimes advantageous for components to have awareness of their size and position on screen. This is useful both for rendering content initially, but also for components that need to know when their size changes. Some examples of this are:

event, but in the interest of building React components that are maximally reusable, it makes sense to encapsulate this logic into the component itself. That way, regardless of where/how the component is used, it will function correctly.

Luckily there are a few excellent libraries that help solve this problem. Here, I’ll give a brief introduction to each and explain when to use it.

function that is called with the element’s contentRect (which has bounds and position). This method is called whenever the component’s size or position changes, and can be used to trigger side effects or put the dimensions into state.

function, so it’s not entirely possible for the component to have render logic based on its dimensions.

but uses a clever solution to solve the aforementioned problem.

logic based on its own dimensions!

looks like this:

doesn’t do this initial render, it should ideally be used in these cases.

The methods that both libraries use to detect size changes also have a slight latency (~20ms). Usually this feels quite fast, but in practice, I’ve noticed a slight delay in detecting changes which can lead to a component feeling slow.

is a good option.

LogRocket is a frontend logging tool that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.

In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single page apps.

Building Size-Aware React Components – LogRocket