react-codemirror2 is a React component wrapper for CodeMirror 5, designed to embed a customizable code editor into React applications. It provides both `Controlled` and `UnControlled` components, catering to different state management paradigms within React. The current stable version is 9.0.1. Its release cadence is primarily tied to major React version updates for peer dependency compatibility and critical bug fixes, rather than a fixed schedule. A key differentiator is its explicit commitment to CodeMirror 5, meaning it does not support the fundamentally rewritten CodeMirror 6 API. Developers must manually import CodeMirror modes and themes. The library ships with TypeScript types and is widely used for creating syntax-highlighting text areas.
npm install react-codemirror2Verified import paths — ran on the pinned version, not inferred.
Demonstrates a controlled React component wrapping CodeMirror 5, showing how to manage editor state and include necessary CSS and language modes. This setup allows external control over the editor's content.
Upgrade your React installation to `^19.x.x` or ensure compatibility if using an older `react-codemirror2` version.
Upgrade your React installation to `^18.x.x`.
Update your event handler signatures for `copy`, `cut`, and `paste` to remove the fourth `event` parameter, typically leaving `(editor, data, value)`.
If you require CodeMirror 6, you must use a different wrapper library, such as `react-codemirror` (without the '2'), or integrate CodeMirror 6 directly.
For Vite, consider adding `react-codemirror2` and `codemirror` to `optimizeDeps.include` in `vite.config.js` and potentially using `@rollup/plugin-commonjs`.
Always include `import 'codemirror/lib/codemirror.css';` and `require('codemirror/mode/<mode>/<mode>.js');` (or similar for themes) in your project.Ensure `import 'codemirror/lib/codemirror.css';` is present, and `require('codemirror/mode/<your-mode>/<your-mode>.js');` is called for each mode used. Also check `options.mode`.For controlled components, ensure `onBeforeChange={(editor, data, value) => this.setState({ value })}` (or equivalent for functional components) is implemented to update the parent component's state.For Vite, configure `vite.config.js` with `optimizeDeps: { include: ['react-codemirror2', 'codemirror'] }` and consider using `@rollup/plugin-commonjs` if issues persist during build.