Skip to content

Commit 985cf48

Browse files
complete document and examples
1 parent 1d7287e commit 985cf48

File tree

5 files changed

+73
-45
lines changed

5 files changed

+73
-45
lines changed

README.md

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ function App() {
5050
return (
5151
<StringToReactComponent>
5252
{`(props)=>{
53-
const {useState}=React;
54-
const [counter,setCounter]=useState(0);
53+
const [counter,setCounter]=React.useState(0); // by default your code has access to the React object
5554
const increase=()=>{
5655
setCounter(counter+1);
5756
};
@@ -69,25 +68,25 @@ function App() {
6968

7069
- The given code inside the string should be a function.
7170

72-
- 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 :
73-
74-
```js
75-
import {useState} from 'react';
76-
import StringToReactComponent from 'string-to-react-component';
77-
function App() {
78-
return (
79-
<StringToReactComponent data={{useState}}>
80-
{`(props)=>{
81-
console.log(typeof useState); // undefined
82-
console.log(typeof React.useState); // function
83-
console.log(typeof props.useState); // function
84-
...
85-
86-
}`}
87-
</StringToReactComponent>
88-
);
89-
}
90-
```
71+
- The code inside the string has access to the `React` object and for using `useState`, `useEffect`, `useRef` and ... you should get them from `React` object or pass them as `data` prop to the component:
72+
73+
```js
74+
import {useState} from 'react';
75+
import StringToReactComponent from 'string-to-react-component';
76+
function App() {
77+
return (
78+
<StringToReactComponent data={{useState}}>
79+
{`(props)=>{
80+
console.log(typeof useState); // undefined
81+
console.log(typeof React.useState); // function
82+
console.log(typeof props.useState); // function
83+
...
84+
85+
}`}
86+
</StringToReactComponent>
87+
);
88+
}
89+
```
9190

9291
## Using Unknown Elements
9392

@@ -114,36 +113,43 @@ function App() {
114113

115114
### data
116115

117-
- type : object
118-
- not required
116+
- type : `object`
117+
- required : `No`
119118
- `data` object is passed to the component(which is generated from the string) as props
119+
- example :
120+
121+
```js
122+
import {useState} from 'react';
123+
import StringToReactComponent from 'string-to-react-component';
124+
function App() {
125+
const [counter, setCounter] = useState(0);
126+
const increase = () => {
127+
setCounter(counter + 1);
128+
};
129+
return (
130+
<StringToReactComponent data={{counter, increase}}>
131+
{`(props)=>{
132+
return (<>
133+
<button onClick={props.increase}>+</button>
134+
<span>{'counter : '+ props.counter}</span>
135+
</>);
136+
}`}
137+
</StringToReactComponent>
138+
);
139+
}
140+
```
120141

121142
### babelOptions
122143

123-
- type : object
124-
- not required
144+
- type : `object`
145+
- required : `No`
146+
- default value : `{presets: ["react"],sourceMaps: "inline"}`
125147
- See the full option list [here](https://babeljs.io/docs/en/options)
126148
- examples :
127-
- using source map :
128-
```js
129-
<StringToReactComponent babelOptions={{filename: 'counter.js', sourceMaps: 'inline'}}>
130-
{`(props)=>{
131-
const {useState}=React;
132-
const [counter,setCounter]=useState(0);
133-
const increase=()=>{
134-
setCounter(counter+1);
135-
};
136-
return (<>
137-
<button onClick={increase}>+</button>
138-
<span>{'counter : '+ counter}</span>
139-
</>);
140-
}`}
141-
</StringToReactComponent>
142-
```
143149
- using typescript :
144150
```js
145151
<StringToReactComponent
146-
babelOptions={{filename: 'counter.ts', presets: [['typescript', {allExtensions: true, isTSX: true}]]}}>
152+
babelOptions={{filename: 'counter.ts', presets: ['react', ['typescript', {allExtensions: true, isTSX: true}]]}}>
147153
{`()=>{
148154
const [counter,setCounter]=React.useState<number>(0);
149155
const increase=()=>{

example/stories/data-prop/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
```jsx
2+
import {useState} from 'react';
3+
import StringToReactComponent from 'string-to-react-component';
4+
function App() {
5+
const [counter, setCounter] = useState(0);
6+
const increase = () => {
7+
setCounter(counter + 1);
8+
};
9+
return (
10+
<StringToReactComponent data={{counter, increase}}>
11+
{`(props)=>{
12+
return (<>
13+
<button onClick={props.increase}>+</button>
14+
<span style={{padding:'0px 10px'}}>{'counter : '+ props.counter}</span>
15+
</>);
16+
}`}
17+
</StringToReactComponent>
18+
);
19+
}
20+
<App />;
21+
```

example/stories/usage/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function App() {
44
return (
55
<StringToReactComponent>
66
{`(props)=>{
7-
const [counter,setCounter]=React.useState(0);//by default React object is emmbed into your string code
7+
const [counter,setCounter]=React.useState(0);//by default your code has access to the React object
88
const increase=()=>{
99
setCounter(counter+1);
1010
};

example/stories/using-react-hooks/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Your imported object including `useState`, `useEffect`, `useRef` and ... are not accessible inside the string code but you can get them from `React` object ( by default `React` object is emmbed into your string code ) or pass them as the `data` prop
1+
The code inside the string has access to the `React` object and for using `useState`, `useEffect`, `useRef` and ... you should get them from `React` object or pass them as `data` prop to the component
22

33
```jsx
44
import {useState} from 'react';
@@ -10,8 +10,8 @@ function App() {
1010
{`(props)=>{
1111
return (<>
1212
<p>type of imported useState is {typeof useState}</p>
13+
<p>type of React is {typeof React}</p>
1314
<p>type of React.useState is {typeof React.useState}</p>
14-
<p>type of React is {typeof React} ( by default React object is emmbed into your string code )</p>
1515
<p>type of props.useState is {typeof props.useState}</p>
1616
</>);
1717
}`}

styleguide.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module.exports = {
3434
sections: [
3535
{name: 'Minimal Usage', content: 'example/stories/usage/README.md', sectionDepth: 1},
3636
{name: 'Using Unknown Elements', content: 'example/stories/using-unkown-elements/README.md', sectionDepth: 1},
37+
{name: 'data prop', content: 'example/stories/data-prop/README.md', sectionDepth: 1},
3738
{name: 'Using React Hooks', content: 'example/stories/using-react-hooks/README.md'},
3839
{name: 'filename option', content: 'example/stories/filename-option/README.md', sectionDepth: 1},
3940
{name: 'Using Typescript', content: 'example/stories/typescript/README.md', sectionDepth: 1},

0 commit comments

Comments
 (0)