1

I'm new to ace and trying to build an Editor with react-ace.

Here is what I did:

  1. npm install react-ace ace-builds
  2. I added the following code to my App.js
import React from "react";
import { render } from "react-dom";
import AceEditor from "react-ace";

import "ace-builds/src-noconflict/mode-java";
import "ace-builds/src-noconflict/theme-github";

function onChange(newValue) {
  console.log("change", newValue);
}

// Render editor
render(
  <AceEditor
    mode="java"
    theme="github"
    onChange={onChange}
    name="UNIQUE_ID_OF_DIV"
    editorProps={{ $blockScrolling: true }}
  />,
  document.getElementById("example")
);

However my browser shows this error: ReferenceError: ace is not defined enter image description here

Here is my package.json: enter image description here

Can you help me please. Thank you!

Johnny Willemsen
  • 2,942
  • 1
  • 14
  • 16
abdellah
  • 65
  • 1
  • 8

4 Answers4

3

Add this code

import 'ace-builds/src-noconflict/ace';

To learn more, see our https://github.com/securingsincity/react-ace/issues/1233

odyu
  • 39
  • 2
0

There is no DOM element with example id. You are using create-react-app so you need to replace line:

document.getElementById("example")

to

document.getElementById("root")
Max Arzamastsev
  • 515
  • 1
  • 5
  • 7
-1

Firstly, in App.js don't need to use again render function, because already use in index.js component. For example, your code will look like this:

import React from 'react';
import AceEditor from "react-ace";
import "ace-builds/src-noconflict/mode-java";
import "ace-builds/src-noconflict/theme-github";

const App = () => {

    const onChange = (newValue) => {
        console.log("change", newValue);
      }

    return (
        <AceEditor
            mode="java"
            theme="github"
            onChange={onChange}
            name="UNIQUE_ID_OF_DIV"
            editorProps={{ $blockScrolling: true }}
        />
    );
}

export default App;
Tabish Adnan
  • 104
  • 4
-1

make sure you are downloading ace-master, here is the link: https://github.com/ajaxorg/ace-builds.

john.
  • 3
  • 1
  • Can you provide more detail on how to use `ace-master` to solve the issue? – Romeo Jun 26 '22 at 20:07
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/32106840) – treckstar Jul 02 '22 at 23:18