React-Ace Editor Component
React-Ace provides a set of React components that wrap the Ace Editor, a high-performance code editor, allowing developers to easily embed sophisticated code editing capabilities into their React applications. The current stable version is 14.0.1, which includes support for React 19. The project demonstrates an active release cadence with frequent minor and patch updates, alongside periodic major version bumps to incorporate new features, bug fixes, and dependency upgrades, often aligning with updates to `ace-builds` itself. Key differentiators include its robust API for controlling editor behavior, support for various modes, themes, and extensions through the `ace-builds` package, and dedicated components for split view and diff editing, offering a comprehensive solution for interactive code snippets, IDE-like features, or rich text editing with syntax highlighting.
Common errors
-
Error: 'ace' is not defined
cause The Ace Editor global object (`ace`) is not available because `ace-builds` has not been correctly imported or loaded.fixEnsure you have `ace-builds` installed and that the necessary `ace-builds` files are imported, particularly if you are trying to configure `ace.config.setOptions` or access other global Ace properties. -
TypeError: Cannot read properties of undefined (reading 'config')
cause Attempting to access `ace.config` before the `ace-builds` library has initialized the `ace` global object.fixMake sure `import 'ace-builds';` or a specific mode/theme import that initializes the global `ace` object runs before any code attempting to access `ace.config` or similar properties. -
Warning: ace.config.set'setOptions' called before the editor is ready.
cause Trying to apply editor options via `setOptions` prop or `ace.config.setOptions` too early, before the AceEditor component or the Ace library itself has fully initialized.fixFor component-specific options, use the `setOptions` prop directly on the `AceEditor` component. For global Ace configurations, ensure `ace-builds` imports are at the top of your main entry file or explicitly manage their loading order.
Warnings
- breaking Version 8.0.0 of `react-ace` dropped support for `brace` and now directly depends on `ace-builds`. This requires updating your imports and potentially refactoring how modes, themes, and extensions are loaded.
- gotcha Modes, themes, and extensions for Ace Editor are not bundled with `react-ace`. They must be explicitly imported from `ace-builds` for the editor to function correctly with specific languages or visual styles.
- deprecated The `$blockScrolling` property within `editorProps` is a legacy setting from older Ace Editor versions. While still supported, it's often recommended to avoid it or address the underlying reasons for its use (e.g., complex layout issues) in modern React development, as it can sometimes lead to performance concerns or unexpected behavior.
- gotcha Updating `react-ace` to a new major version (e.g., v13 to v14) may introduce updated `peerDependencies` requirements for React itself, or updated `ace-builds` versions. Failing to update these alongside `react-ace` can lead to runtime errors or unexpected behavior.
Install
-
npm install react-ace -
yarn add react-ace -
pnpm add react-ace
Imports
- AceEditor
import { AceEditor } from 'react-ace'; // Incorrect named importimport AceEditor from 'react-ace';
- setOptions
import { setOptions } from 'react-ace'; // Not directly exported by react-aceimport ace from 'ace-builds'; ace.config.setOptions({ useWorker: false }); - mode-javascript
import { javascript } from 'ace-builds/src-noconflict/mode-javascript';import 'ace-builds/src-noconflict/mode-javascript';
Quickstart
import React from "react";
import { createRoot } from "react-dom/client";
import AceEditor from "react-ace";
import "ace-builds/src-noconflict/mode-javascript";
import "ace-builds/src-noconflict/theme-github";
import "ace-builds/src-noconflict/ext-language_tools";
function MyAceEditor() {
const onChange = (newValue) => {
console.log("Editor content changed:", newValue);
};
return (
<AceEditor
mode="javascript"
theme="github"
onChange={onChange}
name="code-editor-instance"
editorProps={{ $blockScrolling: true }}
setOptions={{
enableBasicAutocompletion: true,
enableLiveAutocompletion: true,
enableSnippets: true,
showLineNumbers: true,
tabSize: 2,
}}
style={{
width: '100%',
height: '300px'
}}
placeholder="Start coding here..."
/>
);
}
const container = document.getElementById('example');
if (container) {
const root = createRoot(container);
root.render(<MyAceEditor />);
} else {
console.error("Root element 'example' not found.");
}