I’m using this tutorial to create what I understand is called a “single page” React application.
$ npx create-react-app first-react-app
Creating a new React app in /home/username/first-react-app.
Installing packages. This might take a couple of minutes.
Installing react, react-dom, and react-scripts with cra-template...
...
Success! Created first-react-app at /home/username/first-react-app
Here’s what src/App.js looks like:
$ cat src/App.js
import logo from './logo.svg';
import './App.css';
function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
export default App;
I ran it and I can open the default page in my browser from my local machine:
$ cd first-react-app
$ npm run start
$ npm run start
> [email protected] start
> react-scripts start
Compiled successfully!
You can now view first-react-app in the browser.
Local: http://localhost:3000
On Your Network: http://192.168.56.1:3000
Ultimately I want to test the draft.js WYSIWYG editor, so I attempted to install it as a package(?)– I’m new to React so I’m not sure about the nomenclature. So I’m following the docs for installing draft.js from here.
$ npm install draft-js react react-dom
added 12 packages, and audited 1428 packages in 4s
So that seems to have run okay. To ensure I still have a good project, I ran npm run start again, and I get a working local website still.
So now onto actually creating a draft.js WYSIWYG. The default page from the react app says Edit src/App.js and save to reload. So now I’m copy/pasting the instructions from the draft.js documentation, to try to get the WYSIWYG in my react app. Here’s my attempt to add draft:
$ cat src/App.js
import logo from './logo.svg';
import './App.css';
import React from 'react';
import ReactDOM from 'react-dom';
import {Editor, EditorState} from 'draft-js';
import 'draft-js/dist/Draft.css';
function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
class MyEditor extends React.Component {
constructor(props) {
super(props);
this.state = {editorState: EditorState.createEmpty()};
this.onChange = editorState => this.setState({editorState});
}
render() {
return (
<Editor editorState={this.state.editorState} onChange={this.onChange} />
);
}
}
ReactDOM.render(<MyEditor />, document.getElementById('container'));
export default App;
And I also added <div id="container"></div> to public/index.html, since that’s what the instructions are targeting. When I run this, it compiles without error or warning. However when the react app loads in the browser, there is no draft.js WYSIWYG displayed.
In the console, I get this error message on reload: Warning: ReactDOM.render is no longer supported in React 18. Use createRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. Learn more: https://reactjs.org/link/switch-to-createroot
So I updated my code to this:
+import { createRoot } from 'react-dom/client';
import {Editor, EditorState} from 'draft-js';
import 'draft-js/dist/Draft.css';
@@ -40,7 +41,9 @@ class MyEditor extends React.Component {
}
}
-ReactDOM.render(<MyEditor />, document.getElementById('container'));
+const container = document.getElementById('container');
+const root = createRoot(container);
+root.render(<MyEditor />);
And now when I run it, I get this error in the console:
Uncaught Error: createRoot(…): Target container is not a DOM element.
I believe it’s trying to target <div id="container">, but I’m not 100% sure.
So, long story long, how do I integrate the draft.js library into my React project? Am I even starting off on the right foot?
Here are the relevant versions of installed software:
$ npm --version
8.5.5
$ node --version
v16.15.0
$ npm list
[email protected] /home/slefevre/first-react-app
├── @testing-library/[email protected]
├── @testing-library/[email protected]
├── @testing-library/[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]