ReferenceError: ace is not defined - reactjs

I'm new to ace and trying to build an Editor with react-ace.
Here is what I did:
npm install react-ace ace-builds
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
Here is my package.json:
Can you help me please. Thank you!

Add this code
import 'ace-builds/src-noconflict/ace';
To learn more, see our https://github.com/securingsincity/react-ace/issues/1233

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")

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;

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

Related

Modal Form: react__WEBPACK_IMPORTED_MODULE_0__.createPortal is not a function

I'm working on shopping Cart app, I want to use a Modal Form to display the cart's content and additional options before place the order, that's why I'm using Portals, so far, the source code of my Modal.js looks like this:
import { Fragment } from 'react';
import ReactDOM from 'react';
//import ReactDOM from 'react-dom/client';
import classes from './Modal.module.css';
const Backdrop = (props) => {
return <div className={classes.backdrop} onClick={props.onClose}/>;
};
const ModalOverlay = (props) => {
return (
<div className={classes.modal}>
<div className={classes.content}>{props.children}</div>
</div>
);
};
const portalElement = document.getElementById('overlays');
const Modal = (props) => {
return (
<Fragment>
{ReactDOM.createPortal(<Backdrop onClose={props.onClose} />, portalElement)}
{ReactDOM.createPortal(
<ModalOverlay>{props.children}</ModalOverlay>,
portalElement
)}
</Fragment>
);
};
export default Modal;
When I tried to load the modal form -clicking on an icon- I get this error:
This is the React's version I'm using:
This code used to work on previous version of React (17.x), the weird thing I tried to downgrade but still getting the same error.
My questions are:
In ver 18.x of React, Portals have been changed?
How can I downgrade React properly in order to test my code?
do you have any other suggestions how to overcome this issue using React's 18?
Thanks a lot
I had same error and I solved by importing
import ReactDOM from 'react-dom';
instead of
import ReactDOM from 'react-dom/client';

How to fix React forwardRef(Menu) Material UI?

I created very simple custom components MuiMenu and MuiMenuItem. But when I try displaying these components I see an error as shown below:
Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
Check the render method of `ForwardRef(Menu)`.
But if I import directly from #material-ui/core - all is well. How do I fix this?
Working example:
https://codesandbox.io/s/sharp-shadow-bt404?file=/src/App.js
As the error says, Material-UI is using ForwardRef and you need to include that in your code. Below is a fix to your MuiMenu and MuiMenuItem Components;
MuiMenu
import React from "react";
import Menu from "#material-ui/core/Menu";
const MuiMenu = React.forwardRef((props, ref) => {
return <Menu ref={ref} {...props} />;
});
export default MuiMenu;
MuiMenuItem
import React from "react";
import MenuItem from "#material-ui/core/MenuItem";
const MuiMenuItem = React.forwardRef((props, ref) => {
return <MenuItem ref={ref} {...props} />;
});
export default MuiMenuItem;
Also there was an error with the strictmode you used at index so I removed it.
Index.JS
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
const rootElement = document.getElementById("root");
ReactDOM.render(
<App />,
rootElement
);
here is a link to the fixed sandbox: https://codesandbox.io/s/hopeful-thunder-u6m2k
And here are other links to help you understand a bit more: https://material-ui.com/getting-started/faq/#how-do-i-use-react-router | https://reactjs.org/docs/react-api.html#reactforwardref

Formio UI not displaying correctly with ReactDOM

I'm new to react and I'm learning to use hooks, but I can't seem to get the Formio plugin to work correctly with a component.
I'm not sure how React.DOM would be implemented with hooks, but Formio's docs state:
import React from 'react';
import ReactDOM from 'react-dom';
import {FormBuilder} from 'react-formio';
ReactDOM.render(
<FormBuilder form={{display: 'form'}} onChange={(schema) => console.log(schema)} />
, document.getElementById('builder')
);
I tried implementing on a component, but ReactDOM kept giving me an error. This code works, but the plugin is all wonky so I know something isn't working correctly.
import React, { useState, useEffect } from "react";
import ReactDOM from 'react-dom';
import { FormBuilder } from 'react-formio';
const FormBuilderPage = props => {
return (
<FormBuilder
form={{ display: 'form' }}
onChange={(schema) => console.log(schema)}
/>
//,document.getElementById('builder')
)
// This returns an error
return (
ReactDOM.render(
<FormBuilder form={{display: 'form'}} onChange={(schema) => console.log(schema)} />
, document.getElementById('builder')
)
)
}
export default FormBuilderPage;
When I add ReactDOM.render I get the error:
Uncaught Error: Target container is not a DOM element.
Any advise would help!
So their documentation wasn't very clear, but React.DOM is not needed. There was just a css import that I had to add to the index.html file.
So this works!
import React from "react";
import { FormBuilder } from 'react-formio';
const FormBuilderPage = props => {
return (
<FormBuilder
form={{ display: 'form' }}
onChange={(schema) => console.log(schema)}
/>
)
}
export default FormBuilderPage;
<!-- Formio CSS-->
<link
rel="stylesheet"
href="https://unpkg.com/formiojs#latest/dist/formio.full.min.css"
/>

React SVG tag name provided is not valid

I'm attempting to add an SVG to a React app (built with create-react-app). When I try to add it as a component, I get an error like this:
InvalidCharacterError: Failed to execute 'createElement' on 'Document': The tag name provided ('/static/media/logo.8d229b2c.svg') is not a valid name.
What am I missing?
Code:
import React from 'react';
import Logo from '../img/logo.svg';
const Header = () => {
return (
<div>
<Logo />
</div>
)
}
export default Header;
You can import it this way:
import { ReactComponent as Logo } from '../img/logo.svg';
as said in CRA (create-react-app) documentation
an render it the way you want:
import React from 'react';
import { ReactComponent as Logo } from '../img/logo.svg';
const Header = () => {
return (
<div>
<Logo />
</div>
)
}
And also, if it's not necessary to render it as a component, you could just render your svg as an image this way:
import React from 'react';
import logo from '../img/logo.svg';
const Header = () => {
return (
<div>
<img src={logo} alt="logo"/>
</div>
)
}
export default Header;
You need to import the component using this syntax:
import { ReactComponent as Logo } from '../img/logo.svg';
Using the curly braces and ReactComponent is necessary - they tell React that you want to build a component with the SVG.
I only found this because of a Dan Abramov reply to a create-react-app issue. The link he posted in his comment no longer works, but it's still mentioned in the docs.
https://github.com/facebook/create-react-app/issues/5293
https://create-react-app.dev/docs/adding-images-fonts-and-files/

React-hot-loader with material-ui

i use in my project #material-ui and react-hot-loader.
Hot loader works fine, but if i use material-ui modules, it not works.
import React from 'react';
import { hot } from 'react-hot-loader';
import DeleteIcon from '#material-ui/icons/Delete'; // TODO
const App = () => {
return (
<div>
text
</div>
);
};
export default hot(module)(App);
May be, someone know, why? What i need for a correct working? It problem only with #material-ui.
Thanks you for attention!

Resources