Comments (2)
In React, you can achieve this by using the useRef hook instead of useMemo. The useRef hook allows you to create a mutable reference that persists across renders and allows you to interact with the DOM directly. Here's how you can do it:
import React, { useRef, useEffect } from 'react';
const MyComponent = () => {
const elementRef = useRef(null);
useEffect(() => {
if (elementRef.current) {
// Manipulate the DOM element directly here if needed
// For example, you can add event listeners, set attributes, etc.
elementRef.current.innerHTML = 'Hello World';
}
// If you need to perform any cleanup when the component unmounts,
// you can return a cleanup function from useEffect.
return () => {
// Perform cleanup if needed (e.g., remove event listeners)
};
}, []);
return (
<React.Fragment>
<div ref={elementRef} />
</React.Fragment>
);
};
export default MyComponent;
With this approach, you can have a reference to the DOM element that persists across renders, and you can update the DOM directly inside the useEffect hook. Keep in mind that directly manipulating the DOM should be used judiciously in React to avoid interfering with React's internal rendering and reconciliation mechanisms.
As for your use case of a Rich Text Editor, you can combine event handling with useRef to maintain state inside the DOM while also listening to events and performing actions based on user interactions. However, be cautious with this approach, as mixing direct DOM manipulation with React's virtual DOM may lead to unexpected issues and bugs. It's crucial to strike the right balance between React's state management and direct DOM manipulation to avoid conflicts and maintain a clean and efficient codebase.
from react.
Of course I know about useRef
Drawbacks
1 - Makes the ref nullable
2 - You can only use it inside the use effect, for example I have a sibling component that requires the created div as a prop
you can use useRef to append a child you created before, Which is what I do since I need to send my div as a prop to toolbar
I can set a state too
The solution I propose , opens so many doors to ease of use and everything , just wanna know from some react developer if it's something they'd consider in jsx
from react.
Related Issues (20)
- React Dev Tools doesn't work HOT 18
- ADD Suppport for VEDV (https://github.com/yunielrc/vedv)
- Bug: `srcSet` prop is rendered to `srcSet` attribute (instead of `srcset`) HOT 6
- ใ ใดใ ใน
- Control blocking vs lazy behavior of Suspense during renderToPipeableStream HOT 5
- Bug: Can't use bigint for the key property HOT 15
- how to use react developer tools in chrome HOT 1
- [Feature Request]: RSC error - allow error.code or an other specialized prop to be forwarded to the client HOT 1
- Bug: Components which doesn't consume Context are re-rendered when context state is updated HOT 1
- Bug: HOT 1
- Bug: react-dom/server.bun in `@canary` hangs without flushing or terminating the connection on long class names(?) HOT 3
- Bug: react-test-renderer types of .toTree API are missing HOT 4
- Bug: missing button data in form when submitted via `formAction` HOT 10
- Is react abandoned? HOT 7
- Bug: Suspense does not work in Server Components with `use` api
- Regarding component splitting in react... HOT 5
- Bug: react native android shaking on scroll HOT 1
- Bug: renderToString and renderToStaticMarkup skipping children for HTML custom elements HOT 4
- Bug: Lockdown Failed HOT 13
- react devtools HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react.