Skip to content

Commit 8977d4a

Browse files
React global variable should be create
inside the component
1 parent 45e4deb commit 8977d4a

File tree

4 files changed

+6277
-6137
lines changed

4 files changed

+6277
-6137
lines changed

src/ctx.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
class Ctx {
2-
constructor() {
2+
constructor(React) {
33
this._temp = '';
44
this._parentTemp = `"use strict";\nreturn @temp;`;
55
this._com = null;
6+
window.React = window.React || React;
67
if (!(Object.prototype.hasOwnProperty.call(window, 'Babel') && typeof window.Babel === 'object')) {
78
throw new Error(`string-to-react-component package needs @babel/standalone for working correctly.
89
you should load @babel/standalone in the browser.`);

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import Ctx from './ctx.js';
22
import StringToReact from './strintToReact.js';
3-
const getCtx = () => new Ctx();
3+
const getCtx = (options) => new Ctx(options);
44
export default StringToReact.bind(null, {getCtx});

src/strintToReact.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import React, {useRef} from 'react';
1+
import React, {useRef } from 'react';
22
import PropTypes from 'prop-types';
3-
window.React = window.React || React;
43
function StringToReactComponent({getCtx}, props) {
54
const ref = useRef(null);
6-
if (!ref.current) {
7-
ref.current = getCtx();
8-
}
5+
ref.current = ref.current || getCtx(React);
96
const babelOptions = props.babelOptions || {};
107
const GeneratedComponent = ref.current.updateTemplate(props.children, babelOptions).getComponent();
118
const data = props.data || {};

0 commit comments

Comments
 (0)