Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/ziven27/jsx2string

Compile JSX to plain javascript strings, without React or other runtime libraries.
https://github.com/ziven27/jsx2string

jsx

Last synced: about 4 hours ago
JSON representation

Compile JSX to plain javascript strings, without React or other runtime libraries.

Awesome Lists containing this project

README

        

## jsx2string

Transpile JSX to plain javascript strings, without React or other runtime libraries.

| NPM package | github |
| ------------------------------------ | ------------------------------- |
| [![npm package][npm-badge]][npm-url] | [![github][git-badge]][git-url] |

[npm-badge]: https://img.shields.io/npm/v/jsx2string.svg
[npm-url]: https://www.npmjs.org/package/jsx2string
[git-url]: https://github.com/ziven27/jsx2string
[git-badge]: https://img.shields.io/github/stars/ziven27/jsx2string.svg?style=social

### Features

- Render Basic Single Component `

`
- ClassName to class `
`
- Ignore `false` props ``
- Conditional Component `{condition ? : }`
- Component with Data Attributes `
`
- Component with Attributes ``
- Nested Component ul>li>a
- Siblings Components ul>li\*3
- Components with classname p.chan
- Map components & numbers `array.map(item =>
{item}
)`
- Fragment `<>hello world> hello world`
- Component Props ``
- Component Children `children`
- [dangerouslySetInnerHTML](#dangerouslysetinnerhtml)

### How To Install

1. The required packages are `@babel/plugin-syntax-jsx`, `@babel/plugin-transform-react-jsx` and of course `jsx2string`。
2. Need `@babel/core`, webpack or any other way to transpile the code that you prefer.

```sh
$ npm install jsx2string @babel/plugin-syntax-jsx @babel/plugin-transform-react-jsx
```

### Getting started

Make sure you have the "runtime" and "importSource" fn defined like below.

```json
// .babelrc
{
"presets": [
"@babel/preset-env"
],
"plugins": [
"@babel/plugin-syntax-jsx",
[
"@babel/plugin-transform-react-jsx",
{
"runtime": "automatic",
"importSource": "jsx2string"
}
]
]
}
```

Now you can create components e.g.

```jsx
import jsx2string, {Fragment} from "jsx2string";

const Avatar = ({children, title = "avatar", ...rest}) => {
return {children};
};

const dangerText=`hello`;

const Home = function () {
const user = {
"firstName": "hello",
"lastName": `world
`
};
return jsx2string(





<>{null}>


{dangerText}

{() => 'hello'}

{['1', '2', '3']}




{[user.firstName, user.lastName].join(" ")} {dangerText} hello


1232
{[1, 2, 3].map((item) => {item})}

)
};

export default Home;
```

> It just looks like React,but it's totally plain javascript.

### dangerouslySetInnerHTML

```jsx
const dangerText=`hello`;

function render() {
return

this children will be ignore

}
```