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…
Continue reading “Building Size-Aware React Components – LogRocket”

Just published a small library to make performant masonry layout lists with ReactNative

  • Allows creating masonry style list layouts in a performant way.
  • This component leverages to render performant masonry layout lists.
  • This component supports most of the props of plus a few extras one:

    Returns the height for a specific item.

Contribute to react-native-masonry-list development by creating an account on GitHub.
Continue reading “Just published a small library to make performant masonry layout lists with ReactNative”

Get the Dimensions of a React Native Phone Screen

Get the dimensions of a #ReactJS Native phone screen:

  • Your link to unlock this lesson will be sent to this email address.
  • In the lesson we’ll use Dimensions to get the width and height of the screen.
  • Member comments are a way for PRO Members to communicate, interact, and ask questions about a lesson.
  • 1:10 React lesson by Jason Brown
  • Unlock this lesson and all 704 of the free egghead.io lessons, plus get React content delivered directly to your inbox!

In this lesson we’ll use Dimensions to get the width and height of the screen. Also we’ll use onLayout to detect orientation changes.
Continue reading “Get the Dimensions of a React Native Phone Screen”