Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/clumsyme/babel-plugin-react-auto-props

auto add props to your React Component
https://github.com/clumsyme/babel-plugin-react-auto-props

auto babel babel-plugin props react

Last synced: 2 months ago
JSON representation

auto add props to your React Component

Awesome Lists containing this project

README

        

# babel-plugin-react-auto-props

English | [中文](/README.zh.md)

## What's this?

This is a babel plugin which auto adds props to your React Component based on your component type.

```jsx
// what we write


Click me

Hello world


// ⚙⚙⚙⚙⚙⚙⚙⚙⚙⚙⚙

// what we get after compile


Click me


Hello world



```

## How can I use it?

Firstly, install it from npm:

```
npm install -D babel-plugin-react-auto-props
```

Secondly, use it in you babel config:

```js
// babel.config.js
const plugins = [
// ...your other babel plugins
[
'babel-plugin-react-auto-props',
{
Button: {
className: 'my-button',
},
p: {
style: {
color: 'grey',
fontSize: 15,
},
},
},
],
]
```

That's it.

## What's the result of the plugin

Given the following React element

```jsx
let element = (


Click me

Hello world



)
```

We will get:

```js
var element = _react.default.createElement(
'div',
null,
_react.default.createElement(
Button,
_defineProperty(
{
className: 'my-button',
},
'className',
'my-button',
),
'Click me',
),
_react.default.createElement(
'p',
_defineProperty(
{
style: {
color: 'grey',
fontSize: 15,
},
},
'style',
{
color: 'grey',
fontSize: 15,
},
),
'Hello world',
),
)
```

## Should I use it?

This is an experimental project, and auto add props to React Component is anti-pattern. You should consider to use [Specialization](https://reactjs.org/docs/composition-vs-inheritance.html#specialization) in production. However, it's all up to you.

## License

MIT

✨🍰✨