1
- # string-to-react-component
1
+ # string-to-react-component
2
2
3
3
Create React component from string
4
4
@@ -22,16 +22,19 @@ Create React component from string
22
22
23
23
## Installation
24
24
25
- First You need to load ` @babel/standalone ` in the browser :
26
-
27
25
``` js
28
- < script src= " https://unpkg.com/@babel/standalone/babel.min.js" >< / script>
26
+ # with npm
27
+ $ npm install string- to- react- component @babel/ standalone -- save
28
+
29
+ # with yarn
30
+ yarn add string- to- react- component @babel/ standalone
29
31
```
30
32
31
- Then install ` string-to-react-component ` package
33
+ ### CDN Links
32
34
33
35
``` js
34
- $ npm install string- to- react- component -- save
36
+ < script src= " https://unpkg.com/@babel/standalone/babel.min.js" >< / script>
37
+ < script src= " https://unpkg.com/string-to-react-component@3.1.0/dist/stringToReactComponent.umd.min.js" >< / script>
35
38
```
36
39
37
40
## Basic Example
@@ -61,18 +64,18 @@ function App() {
61
64
62
65
- The given code inside the string should be a function.
63
66
64
- - The code inside the string is executed in the global scope, so imported objects from ` react ` package including ` useState ` , ` useEffect ` , ... are not accessible inside it and you should get them from ` React ` global variable :
67
+ - The code inside the string is executed in the global scope, so imported objects from ` react ` package including ` useState ` , ` useEffect ` , ... are not accessible inside it and you can get them from ` React ` global variable or pass them as props to the component :
65
68
66
69
``` js
67
70
import {useState } from ' react' ;
68
71
import StringToReactComponent from ' string-to-react-component' ;
69
72
function App () {
70
73
return (
71
- < StringToReactComponent>
72
- {` ()=>{
74
+ < StringToReactComponent data = {{useState}} >
75
+ {` (props )=>{
73
76
console.log(typeof useState); // undefined
74
77
console.log(typeof React.useState); // function
75
-
78
+ console.log(typeof props.useState); // function
76
79
...
77
80
78
81
}` }
@@ -116,38 +119,38 @@ function App() {
116
119
- not required
117
120
- See the full option list [ here] ( https://babeljs.io/docs/en/options )
118
121
- examples :
119
- * using source map :
120
- ``` js
121
- < StringToReactComponent babelOptions= {{filename: ' counter.js' , sourceMaps: ' inline' }}>
122
- {` (props)=>{
123
- const {useState}=React;
124
- const [counter,setCounter]=useState(0);
125
- const increase=()=>{
126
- setCounter(counter+1);
127
- };
128
- return (<>
129
- <button onClick={increase}>+</button>
130
- <span>{'counter : '+ counter}</span>
131
- </>);
132
- }` }
133
- < / StringToReactComponent>
134
- ```
135
- * using typescript :
136
- ` ` ` js
137
- <StringToReactComponent babelOptions={{ filename: 'counter.ts',presets: [["typescript", { allExtensions: true, isTSX: true }]] }}>
138
- { ` () => {
139
- const [ counter , setCounter ] = React . useState < number > ( 0 );
140
- const increase = () => {
141
- setCounter (counter + 1 );
142
- } ;
143
- return ( <>
144
- < button onClick = {increase} > + < / button >
145
- < span > { ' counter : ' + counter} < / span >
146
- < / > );
147
- } ` }
148
- </StringToReactComponent>
149
- ` ` `
150
-
122
+ - using source map :
123
+ ``` js
124
+ < StringToReactComponent babelOptions= {{filename: ' counter.js' , sourceMaps: ' inline' }}>
125
+ {` (props)=>{
126
+ const {useState}=React;
127
+ const [counter,setCounter]=useState(0);
128
+ const increase=()=>{
129
+ setCounter(counter+1);
130
+ };
131
+ return (<>
132
+ <button onClick={increase}>+</button>
133
+ <span>{'counter : '+ counter}</span>
134
+ </>);
135
+ }` }
136
+ < / StringToReactComponent>
137
+ ```
138
+ - using typescript :
139
+ ` ` ` js
140
+ <StringToReactComponent
141
+ babelOptions={{filename: 'counter.ts', presets: [['typescript', {allExtensions: true, isTSX: true}]]}}>
142
+ { ` () => {
143
+ const [ counter , setCounter ] = React . useState < number > ( 0 );
144
+ const increase = () => {
145
+ setCounter (counter + 1 ) ;
146
+ };
147
+ return ( < >
148
+ < button onClick = {increase} > + < / button >
149
+ < span > { ' counter : ' + counter} < / span >
150
+ < / > );
151
+ } ` }
152
+ </StringToReactComponent>
153
+ ` ` `
151
154
152
155
## Caveats
153
156
0 commit comments