Skip to content

Commit 0117a1e

Browse files
2 parents f86a165 + e852616 commit 0117a1e

File tree

6 files changed

+6290
-6142
lines changed

6 files changed

+6290
-6142
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "string-to-react-component",
3-
"version": "3.0.0",
3+
"version": "3.0.2",
44
"private": false,
55
"author": "dev-javascript",
66
"description": "Create React component from string",

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/stringToReact.test.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ beforeAll(() => {
1212
});
1313
beforeEach(() => {
1414
window.Babel = window.Babel || {};
15+
window.React=window.React||React;
1516
renderApp = (temp, deps, rerender, temp2) => {
1617
let secondRender = false;
1718
const StrintToReactCom = StrintToReact.bind(undefined, deps);
@@ -44,7 +45,7 @@ describe('rendering : ', () => {
4445
test('generated component from string should be updated when props.children is changed', () => {
4546
let _ctx, _ctx2;
4647
const getCtx = function () {
47-
_ctx = new Ctx();
48+
_ctx = new Ctx(React);
4849
_ctx.getComponent = jest.fn(() => _ctx._com);
4950
_ctx._transpile = jest.fn(
5051
() => `() => /*#__PURE__*/React.createElement("p", {
@@ -54,7 +55,7 @@ describe('rendering : ', () => {
5455
return _ctx;
5556
},
5657
getCtx2 = function () {
57-
_ctx2 = new Ctx();
58+
_ctx2 = new Ctx(React);
5859
_ctx2.getComponent = jest.fn(() => _ctx2._com);
5960
_ctx2._transpile = jest.fn(
6061
() => `() => /*#__PURE__*/React.createElement("p", {
@@ -73,7 +74,7 @@ describe('rendering : ', () => {
7374
test('it should call updateTemplate method with props.children as a parameter', () => {
7475
let _ctx;
7576
const getCtx = function () {
76-
_ctx = new Ctx();
77+
_ctx = new Ctx(React);
7778
const updateTemplate = _ctx.updateTemplate;
7879
_ctx.updateTemplate = jest.fn((temp) => updateTemplate.call(_ctx, temp));
7980
_ctx._transpile = jest.fn(
@@ -88,7 +89,10 @@ describe('rendering : ', () => {
8889
});
8990
});
9091
describe('React global variable', () => {
91-
test('this package should set the React global variable', () => {
92+
test('The constructor should set the React global variable', () => {
93+
window.React=undefined;
94+
new Ctx(React);
9295
expect(window.React).toEqual(React);
96+
window.React=React;
9397
});
9498
});

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)