# babel-plugin-transform-react-twist **Repository Path**: mirrors_adobe/babel-plugin-transform-react-twist ## Basic Information - **Project Name**: babel-plugin-transform-react-twist - **Description**: Babel transforms for adding Twist features on top of React. - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-09-24 - **Last Updated**: 2026-03-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # babel-plugin-transform-react-twist [![Build Status](https://travis-ci.org/adobe/babel-plugin-transform-react-twist.svg?branch=master)](https://travis-ci.org/adobe/babel-plugin-transform-react-twist) This babel plugin enhances React-compatible JSX with additional features, including structural components, style and class attribute shorthands, and shorthand for two-way data binding. This is intended to be used as part of [React-Twist](https://github.com/adobe/react-twist), but it can also be used as a standalone Babel plugin with React. It also implements various optimizations on top of React, such as automatically hoisting arrow functions from the render function, if safe to do so. ## Quick Reference The plugin options are as follows. If you're using this as part of React-Twist, we recommend using [@twist/react-webpack-plugin](https://github.com/adobe/react-twist-webpack-plugin), which configures this for you. In particular, each Twist library comes with a configuration file that defines its auto-imports, so you don't need to manually add this. ```js { autoImport: { // Components and decorators to be automatically imported (if they're not already). 'ui:button': { module: 'my-ui-library', export: 'SpectrumButton' } }, refAttribute: true, // Support ref={ this.element } syntax (can specify a variable/property, doesn't have to be a function). constructorProps: true, // Add props,context to super() call. styleAttribute: true, // Support style-backround-color={ ... } syntax, and multiple style attributes. classAttribute: true, // Support class-selected={ this.isSelected } syntax, and multiple class attributes. controlFlow: true, // Support structural JSX components: , , , , , namedChildren: true, // Support named children, e.g. My Header{ contents } asAttribute: true, // Support the "as" attribute as a means of providing parameters via JSX (e.g. ...) bindAttribute: true, // Support bind:value={ this.value } as a shorthand for adding an event listener to update this.value. arrowLifting: true, // Automatically lift arrow functions in JSX, so they don't get recreated every time the component is rendered. } ``` ### Structural Components React-Twist provides a set of *structural components* that allow you to write more declarative JSX, to describe the rendering of a component. These structural components, such as ``, ``, and ``, provide basic control-flow constructs. Right now these map onto fairly basic JavaScript equivalents, but we'll be adding further optimizations to React-Twist in the future (e.g. automatically creating sub-components to improve rendering performance). As an example, consider the following render function: ```javascript render() { return

My Items

Fantastic Sale Price!
{ item.name }
; } ``` This is equivalent to: ```javascript render() { return

My Items

{ this.items.map(item => { return [ item.onsale &&
{ item.name }
,
Secret Item
].filter(x => x); }) }
; } ``` The amount of code is very similar, but the declarative style is sometimes easier to read, since it looks closer to an HTML template. React-Twist JSX transforms happen _before_ the React JSX transform, so they're completely optional, and the code they generate is 100% compatible with plain React. > Note: The `` element will soon be deprecated in favor of JSX fragments (`<>`), which is soon landing in React. Some more examples of the syntax: ```jsx // Use if/elseif/else: hello! bonjour! :wave: // Iterate over items in a collection: // The opposite of : Hi // Alias variables with : {item} // Use to group items without wrapping them in a real DOM element: render() { return
One
Two
; } ``` ### Enhanced Class and Style Attributes The built in support for `class` and `style` attributes in React is somewhat limited - the `className` attribute can only take a string, and the `style` attribute can only take an object. Furthermore, you can only have one of each attribute on an element - if you have multiple attributes with the same name, all except the last will be ignored. React-Twist provides some enhancements over React, that make CSS much easier to handle: * Multiple class attributes: `
` maps to `
`. * Boolean class attributes: `
` maps to `
`. * Class attributes as an array: `
` maps to `
`. * Individual style attributes: `
` maps to `
`. (Note that for measurements, React automatically adds "px" if necessary). * Style attributes as strings: `
` maps to `
`. Note that these shorthands become increasingly useful the more you use on the same element - e.g. it saves you having to write complicated string expressions to construct the `className` attribute. Some examples: ```jsx // Use "style-" shorthand, in addition to React's style object:
// Use "class-" shorthand: ; } ``` Then every time the component re-renders, a new closure is created for the arrow function in the `onClick` handler. From React's perspective, it's impossible to know that the function does the same thing, so it has to remove and re-add the event handler. This can lead to performance problems and GC pressure. The solution to this is to "hoist" the function out of render, namely: ```javascript @Bind handleClick() { this.clickCount++ } render() { return ; } ``` But it's easy to forget to do this. React-Twist makes life easier by doing this hoisting for you - so long as it's safe to do so (e.g. if the arrow function references other variables that were defined inside of `render()`, then there's no way around recreating it each time, since these variables could change).