React Runner is a JavaScript library designed for executing and previewing live React code directly within a web application. Currently stable at version 1.0.5, it provides components and hooks to dynamically run arbitrary React code snippets, supporting modern JavaScript features like class fields, arrow functions for event handlers, and `import` statements. Unlike alternatives like `react-live` which use Bublé, React Runner leverages Sucrase for transpilation, offering broader compatibility with contemporary syntax and TypeScript without enforcing strict coding patterns. It facilitates scenarios such as interactive documentation, code playgrounds, and component development environments, offering features like multi-file code execution and server-side rendering support. While there isn't an explicit release cadence mentioned, the 1.x.x versioning implies ongoing maintenance within this major branch.
npm install react-runnerVerified import paths — ran on the pinned version, not inferred.
Illustrates dynamic execution of React code, demonstrating how to provide external components and manage imports within the `scope` for a live preview, including state and interaction.
Ensure target browser environments meet the specified `browserslist` criteria or consider alternative solutions for wider compatibility.
Keep the live code relatively standard. If advanced features are needed, pre-transpile the code before passing it to `react-runner` or ensure the features are supported by Sucrase.
Review the `react-runner` or `react-live-runner` documentation and adapt your code to the new API, especially for props like `scope` and handling imports. Consider using `react-live-runner` for a smoother transition if your existing `react-live` setup is complex.
Ensure all external variables, components (e.g., `React`, `useState`), or modules that the `code` string relies on are explicitly passed within the `scope` object.
Define the `import` object within your `scope` prop, mapping module names to their corresponding values. Use `importCode(localFileContent, baseScope)` for local file imports.
Add `React` (and potentially `ReactDOM` or `useState`, `useEffect` if used as direct imports) to your `scope` prop: `scope={{ React, useState }}`.