question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Functional Components with Exactly Two Arguments Implicitly Pure [Proposal]

See original GitHub issue

Suggestion: I suggest that functional components with exactly 2 arguments to be implicitly pure

Benefit By being implicitly pure instead of wrapped with memo, weird wrapping parenthesis and braces are not required, thus boilerplate is reduced

The Problem Several parenthesis and braces in sequence is difficult to read. For example memo wrapping anonymous functions have two parenthesis and one brace: export default memo(({id}) =>

Closing parenthesis and braces are also required

Considerations

  • Arguments provided by react@next 16.7.0-alpha.2 to a functional components are nextProps and context
  • Advanced React programmers for performance reasons will typically use pure components everywhere
  • Novice React users are unlikely to use context, thus unlikely to list 2 arguments should they use functional components or class components
  • Traditional components today are a legacy pre-15.4 construct, basically the curse of the planet

Implementation

  • The number of arguments a function is declared with are accessible as function-value.length
  • function-value.prototype.isReactComponent is truthy when the function-value is a class component

As of React 16.7.0-alpha.2: “Awkward memo-wrapping”

const Pure1 = memo(function C() {
  …
})
const Pure2 = memo(({id}) => {
  …
})

With suggestion implemented:

const Pure1 = ({id}, pure) =>
  <>
    <h1>ID</h1>
    <p>The id is: {id}</p>
  </>
function Pure2({id}, pure) {
  return <>
    <h1>ID</h1>
    <p>The id is: {id}</p>
  </>
}
const Traditional1 = ({data}) => <div>{data.prop}</div>
function Traditional2({data}, context, trad) {
  return <div>
    {data.prop}
  </div>
}

#14469 was a previous abandoned alternative suggestion

harald.rudell@gmail.com

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
Li357commented, Dec 29, 2018

This seems like a headache especially with linters (no-unused-vars). The fact that it’s implicit makes it very hard to understand at a glance that it’s pure if the second unused argument is named anything else.

3reactions
ckknightcommented, Dec 28, 2018

Why would I want to add an extra argument to a function if I don’t access or otherwise use that argument? Seems confusing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Function Components
This in-depth guide shows you everything about React Function Components -- which are basically just JavaScript Functions being React Components ...
Read more >
What are React pure functional components? - LogRocket Blog
React supports two types of components, class components and functional components. A functional component is a plain JavaScript function that ...
Read more >
Component should be written as a pure function (react prefer ...
To write a functional component, you write a function that takes a single argument. This argument will receive the component's props.
Read more >
Functional Components vs. Class Components in React.js
We've made a full guide on function components in React. Learn about React.js and differentiating Functional vs Class components.
Read more >
All C++20 core language features with examples
So, I decided to read all proposals and create this “cheat sheet” that ... takes two parameters template<typename T1, typename T2> concept C ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found