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”

How I organize my React & Redux projects – Bruno Quaresma – Medium

How I organize my React & Redux projects  #javascript #react #redux #reactjs

  • So, in order to reuse some domain logic like a Payment feature you will need to enter in every function folder and move the respective files and tests to a separeted folder to create a lib.
  • Problems solved… In parts…I think that the views(visual components/dumb components) are not part of the domain because the views are just a way to decorate some data to display to the user.
  • It’s the best part to use and reuse components.Then, I extracted the views from all domains folder and created a components folder in src directory.
  • Therefore, after some reflection, I decided to create a folder called modules and put all the logical domain inside.Another point that I think that is important to care about is the screens folder.
  • The answer to this is because the components folder is for reusable visual components(dumb components) as opposed to screens which are very coupled with the current application.The utils folder is used to keep some logic that can be extracted from the code but is not about the application domain, e.g. string/array operations.If you have a differente point of view, let me know.

In this post, I will share with you how I organize my React & Redux projects and talk about some decisions that I made while working in some projects in the last two years. This structure groups each…
Continue reading “How I organize my React & Redux projects – Bruno Quaresma – Medium”

Structuring React Components – Kacper Goliński – Medium

Structuring React Components  #javascript #reactnative #react #webdevelopment #reactjs

  • React ComponentsIn this short post, I would like to describe how to structure react components.The main idea is to have a structure that helps to build highly maintainable and scalable software and makes it easy for developers to navigate and find files.Each component has its own directory and inside it, we put all related files.
  • It will be also relatively easy to move components around or to create a separate package since all important code will be already packed up.Example componentsWe have to start with components directory which contains “global” components, used in different parts of an application.Above we can see Search / component, which is used in TopNavbar / and somewhere else in the app, that’s why both components are at the same main level.TopNavbar/ directory contains following files and directories:index.js – it contains only single line which exports TopNavbar default.
  • /TopNavbar’;TopNavbar.jsx – main component structure (layout).
  • Private components contain own tests, other private components, and styled-components.
  • Stories for react-storybook are only created for the main component not for private ones.ConclusionAt first, this structure may look complex and overcomplicated.

The main idea is to have a structure that helps to build highly maintainable and scalable software and makes it easy for developers to navigate and find files. Each component has its own directory…
Continue reading “Structuring React Components – Kacper Goliński – Medium”