Adds data-component-trace attribute to html elements showing the trace of React components names that led to this element creation.
Note: it will only work with JSX elements, not with
React.createElement. If you're using TypeScript and transpile it separately, use"jsx": "preserve"in yourtsconfig.jsonand addpreset-reactto your babel presets.
Useful when combined with component-trace-element-finder
Considering components structure:
const HeaderOfComponent = () => <h1 />; const ContentOfComponent = () => <div />; const SecondComponent = () => <><HeaderOfComponent /><ContentOfComponent /></>; const Component = () => <SecondComponent />; render(<Component />);
Resulting HTML markup will look like this:
<h1 data-component-trace=" component second-component header-of-component"></h1> <div data-component-trace=" component second-component content-of-component"></div>
where components names are separated by a single space
attribute- any attribute name you want, should begin fromdata-. Default:data-component-trace.format- can be "camel" (camelCase), "snake" (snake_case) or "kebab" (kebab-case). Default:kebab.separator- can by any non-empty string. Default: (single space, useful for selectors like[data-component-trace~=second-component]).
Example:
["react-component-trace-data-attr", { "attribute": "data-component-trace", "format": "kebab", "separator": " ", }]