Giter VIP home page Giter VIP logo

Comments (9)

kruczy avatar kruczy commented on April 30, 2024 1

@developit thanks for this! that is very helpful, will try this in a sec

from preact.

kruczy avatar kruczy commented on April 30, 2024 1

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.

developit avatar developit commented on April 30, 2024

@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.

developit avatar developit commented on April 30, 2024

@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.

developit avatar developit commented on April 30, 2024

Awesome, and thank you!

from preact.

developit avatar developit commented on April 30, 2024

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.

kruczy avatar kruczy commented on April 30, 2024

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.

developit avatar developit commented on April 30, 2024

Eek! That looks like a caching bug then. Should we open a new issue or use this one?

from preact.

kruczy avatar kruczy commented on April 30, 2024

sure, will do that in a sec

from preact.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.