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.

Use functions instead of classes to define JavaScript components

See original GitHub issue

This requires a major overhaul but would greatly simplify component definitions. Not having to worry about the context of this would ease understanding of how components work and bolster confidence when adding features or items. There are performance-related worries but it looks like those do not translate to real world use cases. React hooks are created using functions and have been adopted without major issues.

As I said, this would be a major undertaking so I was not able to make a pull request but a functional component would resemble existing ones minus the this keyword.

function Alert(element) {
  ...

  function close(element) {
    const rootElement = element ? _getRootElement(element) : _element
    const customEvent = _triggerCloseEvent(rootElement)

    if (customEvent === null || customEvent.defaultPrevented) {
      return
    }

    _removeElement(rootElement)
  }

  ...
}

Here are some articles that explore this subject in greater detail: https://css-tricks.com/the-flavors-of-object-oriented-programming-in-javascript/ https://www.toptal.com/javascript/es6-class-chaos-keeps-js-developer-up

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
jjoel1630commented, May 28, 2021

Yes, I completely understand. Whenever you are willing to make the change, just let me know. I will be happy to help you guys out. In the meantime, I will try to contribute by fixing other issues.

0reactions
luchiancommented, May 28, 2021

@jjoel1630 You can contribute by helping with any of the open issues or by submitting for review your own code implementing some feature.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding Functional Components vs. Class ... - Twilio
First of all, the clear difference is the syntax. Just like in their names, a functional component is just a plain JavaScript function...
Read more >
React component guide: Class vs functional - Educative.io
Using functional components encourages composition, while class components lend themselves to inheritance design patterns. Currently, ...
Read more >
Functional vs Class-Components in React | by David Jöch
The most obvious one difference is the syntax. A functional component is just a plain JavaScript function which accepts props as an argument...
Read more >
Differences between Functional Components and Class ...
A functional component is just a plain JavaScript pure function that accepts props as an argument and returns a React element(JSX). A class...
Read more >
Functional vs Class-Components in React | Quick Guide
Components are similarJavascript functions but work as isolated and return HTML. ... The reason behind this is while using class components.
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