React Artifacts and Content Security Policy
Overview
The React Artifact Viewer allows you to create and preview React components in real-time. However, in production environments with strict Content Security Policy (CSP), JSX transformation may be blocked for security reasons.
Writing CSP-Safe React Components
If you encounter CSP errors when previewing React components, you can write components using React.createElement()
instead of JSX syntax.
Example: Counter Component
JSX Version (May be blocked by CSP)
const Counter = () => {
const [count, setCount] = React.useState(0);
return (
<div style={{ padding: '20px', textAlign: 'center' }}>
<h1>Counter</h1>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
<button onClick={() => setCount(0)}>Reset</button>
</div>
);
};
export default Counter;
CSP-Safe Version (Works everywhere)
const Counter = () => {
const [count, setCount] = React.useState(0);
return React.createElement(
'div',
{ style: { padding: '20px', textAlign: 'center' } },
React.createElement('h1', null, 'Counter'),
React.createElement('p', null, 'Count: ', count),
React.createElement(
'button',
{ onClick: () => setCount(count + 1) },
'Increment'
),
React.createElement(
'button',
{ onClick: () => setCount(0) },
'Reset'
)
);
};
export default Counter;
React.createElement() Syntax
React.createElement(type, props, ...children)
- type: Element type ('div', 'button', etc.) or React component
- props: Object with properties (or null)
- children: Child elements or text content
Security Features
The React Artifact Viewer runs components in an isolated sandbox with:
- No network access: Cannot make API calls or fetch external resources
- No cookie/storage access: Cannot read user data or authentication
- No parent window access: Cannot interact with the main application
- Restricted permissions: Limited to script execution and modals only
Edit Mode
When editing React artifacts, you must explicitly enable "Edit Mode" which:
- Shows a security warning about potential risks
- Allows you to modify the component code
- Only affects your own browser session
- Cannot harm other users or the backend
Best Practices
- Test locally first: Develop components in your local environment
- Use trusted code: Only paste code from trusted sources
- Monitor performance: Watch for infinite loops or excessive memory usage
- Keep it simple: Complex components may be harder to debug in the sandbox