Skip to content

Commit cf77850

Browse files
authored
fix(Renderer): fix timely re-renders (#39)
1 parent 23effcc commit cf77850

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

__tests__/ErrorBoundary-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import React from 'react';
12
import { render } from '@testing-library/react';
23
import ErrorBoundary from '../src/ErrorBoundary';
34

__tests__/Preview-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import React from 'react';
12
import { render } from '@testing-library/react';
23
import Preview from '../src/Preview';
34

__tests__/Renderer-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react';
2+
import { render } from '@testing-library/react';
3+
import Renderer from '../src/Renderer';
4+
5+
it('refault is rendering', () => {
6+
const { container } = render(<Renderer />);
7+
expect(container.querySelector('.rcv-render')).toHaveTextContent('Rendering...');
8+
});
9+
10+
it('should render a test div', () => {
11+
const { container } = render(<Renderer code="ReactDOM.render(<div>test</div>)" />);
12+
13+
expect(container.querySelector('.rcv-render')).toContainHTML('<div>test</div>');
14+
});

src/Renderer.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ const Renderer = React.forwardRef((props: RendererProps, ref: React.Ref<HTMLDivE
8282
} = editor;
8383

8484
const [editable, setEditable] = useState(isEditable);
85-
const [hasError, setHasError] = useState(false);
8685
const [errorMessage, setErrorMessage] = useState(null);
8786
const [compiledReactNode, setCompiledReactNode] = useState(null);
8887

@@ -91,7 +90,6 @@ const Renderer = React.forwardRef((props: RendererProps, ref: React.Ref<HTMLDivE
9190
}, [editable]);
9291

9392
const handleError = useCallback(error => {
94-
setHasError(true);
9593
setErrorMessage(error.message);
9694
}, []);
9795

@@ -123,7 +121,7 @@ const Renderer = React.forwardRef((props: RendererProps, ref: React.Ref<HTMLDivE
123121
});
124122
}
125123
} catch (err) {
126-
console.error(err);
124+
console.warn(err);
127125
} finally {
128126
// Reset the render function to the original value.
129127
ReactDOM.render = originalRender;
@@ -138,11 +136,9 @@ const Renderer = React.forwardRef((props: RendererProps, ref: React.Ref<HTMLDivE
138136

139137
const handleCodeChange = useCallback(
140138
(code?: string) => {
141-
setHasError(false);
142-
setErrorMessage(null);
143139
onChange?.(code);
144-
145140
executeCode(code);
141+
setErrorMessage(null);
146142
},
147143
[executeCode, onChange]
148144
);
@@ -164,6 +160,7 @@ const Renderer = React.forwardRef((props: RendererProps, ref: React.Ref<HTMLDivE
164160
);
165161

166162
const showCodeEditor = editable && code;
163+
const hasError = !!errorMessage;
167164

168165
return (
169166
<div className="rcv-container" {...rest} ref={ref}>

0 commit comments

Comments
 (0)