Comments (9)
@developit thanks for this! that is very helpful, will try this in a sec
from preact.
well @developit you're the most responsive and helpful maintainer I came across 👍 and I mean it!
Just did what you suggested and it works perfectly!
Will throw in a componentDidUnmount
to cleanup after as well
Thanks alot
from preact.
@kruczy The best way to do this is to append the element to this.base
, but you will need to disable component rendering in order to keep that element from being reclaimed (see example below).
You can see this technique in action in preact-token-input - it uses a component as a foothold in the DOM, but then disables updates and lets tags-input take over from there. A more complex example would be preact-richtextarea, which uses this technique to avoid re-rendering an editable <iframe>
.
class DatePicker extends Component {
shouldComponentUpdate() {
// do not re-render via diff:
return false;
}
componentWillReceiveProps(nextProps) {
// you can do something with incoming props here if you need
}
componentDidMount() {
// here's where you can inject the external date picker element:
let datePicker = document.createElement('input');
datePicker.type = 'date';
this.base.appendChild(datePicker);
}
render() {
return <div class="date-picker" />;
}
}
from preact.
@kruczy You're quick! I just threw together a demo of this on Webpackbin:
http://www.webpackbin.com/V1hyNQbpe
I wanted to make sure I wasn't crazy (haha) - the demo shows that even when re-rendering from a parent component (<Example>
), the <DatePicker>
's DOM is preserved.
Cheers! :)
from preact.
Awesome, and thank you!
from preact.
I added this to the Wiki as documentation for others who might have the same question:
https://github.com/developit/preact/wiki/External-DOM-Mutations
from preact.
Turns out there is a problem with componentDidMount
in a reuse scenario
Made a quick example based on yours with a toggle button and cleanup like in my use case
componentDidMount
is called every second time the component is mounted and it should be called every time
see: http://www.webpackbin.com/E1j567bal
from preact.
Eek! That looks like a caching bug then. Should we open a new issue or use this one?
from preact.
sure, will do that in a sec
from preact.
Related Issues (20)
- Add JSX TS aria roles for dpub-aria-1.0
- `TypeError: Cannot set properties of null (setting '__m')` with preact 10.19.4 when destroying a component that calls `useId`
- Problem with rendering child component on props changes HOT 3
- "Components keep being added when updating state" issue persists when using tags other than div HOT 1
- preact/compat's SetStateAction is different from React's SetStateAction HOT 4
- useSelector hook stops being called under certain conditions since preact 10.19.4 HOT 3
- Implement onscrollend event attribute for TypeScript JSX
- TypeScript JSX focus events don't have consistent case HOT 5
- Typescript issues in 10.19.4+ with @mui/material HOT 5
- Provide a boundary between Components and DOM HOT 8
- `preact` `10.9.4+` requires double click or move pointer out of `@headlessui/react` `Tab` panel/button HOT 2
- Unmount hooks should be called during the commit phase to ensure consistency with React HOT 2
- When I attempt to extend HTMLAttributes every intrinsic element becomes of type any HOT 2
- Current plan for event types? HOT 3
- onFocusIn and onFocusOut events incorrectly set HOT 4
- Cannot read properties of undefined (reading '__m')
- `hydrate` doesn't replace attributes at root replaced node HOT 4
- Unable to use hooks in compiled package
- Compatibility issue: Jest mock works in React / fails in Preact when using ForwardRef in a component HOT 2
- Using useState leads to error: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '__H') HOT 3
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 preact.