React: Suggestion: global custom attributes

Created on 25 Sep 2020  路  1Comment  路  Source: facebook/react

Hi,

I would like to suggest this new feature that may be useful: a custom global attribute as a DRY and KISS feature for sharing some repetitive coding or verbose styling. It is a bit familiar to Angular directive concept, but it is more functional and react-like.

  • alternative 1: using @ token in component attribute working as a function injetor
const glyphIcon = (elm, val) => elm.className = "glyphicon glyphicon-" + val + " icon";

<div @glyphIcon="ok" >...</div>
  • alternative 2: function decorators (not support in ecmascript) would be nice for kebab-case notation
@directive("glyph-icon")
const glyphIcon = (elm, val) => elm.className = "glyphicon glyphicon-" + val + " icon";

<div glyph-icon="ok" >...</div>

Most helpful comment

That is something you can already do:

const glyphIcon = (val) => ({ className: "glyphicon glyphicon-" + val + " icon" });

<div {...glyphIcon("ok")} >...</div>

>All comments

That is something you can already do:

const glyphIcon = (val) => ({ className: "glyphicon glyphicon-" + val + " icon" });

<div {...glyphIcon("ok")} >...</div>
Was this page helpful?
0 / 5 - 0 ratings