1- # string-to-react-component
1+ # string-to-react-component
22
33Create React component from string
44
@@ -22,16 +22,19 @@ Create React component from string
2222
2323## Installation
2424
25- First You need to load ` @babel/standalone ` in the browser :
26-
2725``` 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
2931```
3032
31- Then install ` string-to-react-component ` package
33+ ### CDN Links
3234
3335``` 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>
3538```
3639
3740## Basic Example
@@ -61,18 +64,18 @@ function App() {
6164
6265- The given code inside the string should be a function.
6366
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 :
6568
6669``` js
6770import {useState } from ' react' ;
6871import StringToReactComponent from ' string-to-react-component' ;
6972function App () {
7073 return (
71- < StringToReactComponent>
72- {` ()=>{
74+ < StringToReactComponent data = {{useState}} >
75+ {` (props )=>{
7376 console.log(typeof useState); // undefined
7477 console.log(typeof React.useState); // function
75-
78+ console.log(typeof props.useState); // function
7679 ...
7780
7881 }` }
@@ -116,38 +119,38 @@ function App() {
116119- not required
117120- See the full option list [ here] ( https://babeljs.io/docs/en/options )
118121- 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+ ` ` `
151154
152155## Caveats
153156
0 commit comments