Ending the debate on inline functions in React – Flexport Engineering

Ending the debate on inline functions in React  #reactjs

  • In the example above, this.props.onAlert in the callback has the correct reference to this.Why do other people dislike inline functions?An inline function in render allocates a new function instance each time render is called.
  • However, in Flexport’s React app, the garbage collector penalty is dwarfed by another performance issue caused by inline functions: wasteful re-rendering of pure components.Recall from Part 1 of this blog series that pure components rely on a shallow equality comparison in shouldComponentUpdate to determine if any props or state have…
  • Since two different function instances are never equal, inline functions will cause the shallow equality comparison to fail, and thus will always trigger a re-render.
  • So as a React developer, what in the world are you supposed to do?reflective-bind to the rescueThe answer: freely use inline arrow functions in render, and use our ✨new✨ reflective-bind Babel transform to eliminate wasteful re-renders with almost no code change.Total internal reflection.
  • To do that, the Babel plugin hoists the arrow function to the top level, and replaces it with a call to reflectiveBind, passing in the hoisted function and the closed over variables as arguments:With these transforms in place, we can use reflectiveEqual in our shouldComponentUpdate to check for function equality.Performance…

Using inline functions in React is convenient but can be contentious because of their perceived impact on performance. Today Flexport is introducing our solution to the debate: a Babel transform…
Continue reading “Ending the debate on inline functions in React – Flexport Engineering”

A New Way to Handle Events in React

  • In the Facebook docs, you’ll see event handling done like this:

    ES6 classes won’t automatically give scope to , and since you’ll usually want to call or perhaps invoke another method in the component, the “official” convention is to bind all the event handlers all the time in constructor.

  • React Docs

    There’s a much cleaner way to write event handlers that 1) avoids boilerplate and 2) doesn’t cause extra re-renders: property initializer syntax!

  • There’s an ESLint rule that will tell you not to use “bind” or arrow functions in render

    My best guess that Facebook hasn’t “officially” endorsed this pattern in their documentation is because stage-2 ES6 hasn’t been finalized yet, so Property Initializers are still considered non-standards.

  • However, the generator already enables stage-2 so… it’s very likely Property Initializers will become de-facto for defining event handlers in the near future.
  • Once you get comfortable with Property Initializers and begin using them to define handler methods, you’ll gain two notable benefits:

    🌵 Checkout this CodePen to see Property Initializer Syntax in action

Skip boilerplate code and learn how to write more concise event handlers for your React code.
Continue reading “A New Way to Handle Events in React”

Catching exceptions using Higher Order Components in React 16

Catching exceptions using Higher Order Components in #ReactJS 16:  by @notgiorgi #JavaScript

  • So it would be impractical to rewrite the whole component libraries to provide error handling, others may prefer having their views to be “dumb” and represent the UI solely without extra logic.
  • So the better way to go would be wrapping components, reusing wrappers and avoiding modifying the component definitions.So we need to implement this new error handling feature so that:We can display fallback components (views) when the errors happenWe avoid coupling of normal and fallback componentsWe can easily report errors to some serviceWe have ability to reuse reporting or fallback logicNaive, less practical approach would be to do it like this:We could make this slightly better if we parameterize ErrorHandler and instead of calling reportErrorToService directly pass error reporter function as a prop and instead of returning inline fallback view (line 12) we pass a component.
  • But as we already discussed, we don’t want to modify our component libraries, we want to just decorate them.To accomplish this, we could use Higher Order Components:We have this withErrorHandler HOC (We’ll talk about the implementation later), which takes error reporter as callback and fallback component and in case of error, calls the callback and renders the fallback component.
  • We can wrap any component using it and the component will get the error handling, without changing it’s definition, without touching the JSX views.We can make it even prettier, we will probably have one service for error reporting and display mostly the same views as fallback components.
  • So we can export partially applied withErrorHandler HOC like this:and then use it to export wrapped components, instead of just components:This way we can easily add error handling logic to our components and what is also important, we can also easily remove it!So how does the withErrorHandler HOC work?

As you might already know, React 16 beta is out and it ships with a full internal rewrite of the React core (a.k.a Fiber), providing several new features. Though in this post we will only talk about…
Continue reading “Catching exceptions using Higher Order Components in React 16”

Sharing stateful UI logic in React apps using Render Callback components

Sharing stateful UI logic in React apps using Render Callback components



#ReactJS

  • We have three components: an Accordion , Modal , and Thumbnail .
  • We could write each of them as a stateful class component with the same wrapping code, but since they have the same state setup – let’s share it!
  • Let’s abstract the state into a Toggle component (a Render Callback).
  • Now components that use Toggle will have access to their own isOpen and handleToggleClick arguments without having to wire up the state code.
  • The instances of Toggle (Accordion , Modal , and Thumbnail ) can just be stateless function components.

A few examples of abstracting shared logic across multiple components using the Render Callback pattern (aka Function as Child components).
Continue reading “Sharing stateful UI logic in React apps using Render Callback components”

Simple React Router Example – Dan Buda – Medium

Simple React Router Example  #router #reactjs #react #reactjs

  • One of the things I love about React is how easy it is to make components and bring them all together.We’ll need to import a few things from React React Router Router to be able to make it all work.
  • And in our Link tags, we’ve included a to= and a path name that will be used in our Router code.So now for the Router:We use ReactDOM.render to tell our app how the navigation all fits together.
  • First, we want to use browserHistory in the main Router tag, and give our Router a starting point:So our App component, which will house our other components, is set up as the parent route that will use the path of just “/”.
  • Now we can add our Home component using IndexRoute to tell our Router that Home should load first upon getting to the site.The Home component/route is set up inside the App route because we want to set it as a child of App.
  • And now we can add our 2 link pages to the router:They are also added as children of the App component.

Just the basics of using React Router (version 2.8.1 — v4 just came out and I’ll monkey with that soon). So you can use React Router to handle all of your navigation, and once getting used to it, it…
Continue reading “Simple React Router Example – Dan Buda – Medium”

Building User Interfaces with Pure Functions and Function Composition in React

Build user interfaces with pure functions and function composition in #ReactJS:  #JavaScript

  • This idea is so important in React that React 0.14 introduced Stateless Functional Components which allows the code above to be written as normal functions (and which we’ll cover more in depth later in the course).
  • Perhaps one of my favorite things about React is it’s given me a light introduction to functional programming (FP) and a fundamental piece of FP are pure functions.
  • splice is not a pure function since each time we invoke it passing in the same arguments, we get a different result.
  • Well the main reason is React’s render method needs to be a pure function and because it’s a pure function, all of the benefits of pure functions now apply to your UI as well.
  • Another reason is that it’s a good idea to get used to making your functions pure and pushing “side effects” to the boundaries of your program.

👋 I’m Tyler. I’m a Google Developer Expert and a partner at React Training where we teach React online, in person, and build OSS like React Router.
Continue reading “Building User Interfaces with Pure Functions and Function Composition in React”