question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

preact.render does not return component instance

See original GitHub issue

The return value doesn’t appear to be documented for either ReactDOM.render or preact.render, but with ReactDOM an instance of the component can be returned.

Example code via StackOverflow comment:

var Hello = React.createClass({
  render: function() {
    return (this.state) ? <div>Hello {this.state.name}</div> : null;
  }
});

var component = ReactDOM.render(<Hello/>, document.getElementById('container'));
component.setState({name: "World"});
setTimeout(function(){
  component.setState({name: "StackOverFlow"});
}, 1000);

This does not work with Preact; the return value of preact.render appears to be the real DOM rather than the virtual one. Is this intentional?

[I’ll note that using React in this way appears to be frowned upon, at least in that StackOverflow thread I found. But imo it could be handy to have outside access to components, especially with small/store-less apps.]

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
developitcommented, Dec 12, 2017

ah - you should use a ref on the root. That will work in both libs:

let instance;  // holds our component instance
let ref = c => {
  // c is the component instance
  instance = c;
};
render(<Foo ref={ref} />, parent)

console.log(instance)
1reaction
natevwcommented, Dec 12, 2017

@developit D’oh! I had checked preact-compat for this, and even found that method, but somehow completely missed the obvious && out._component part that must have been right under my nose 😦

+1 for wontfixing this — <del>I’m content doing my own ._component lookup even if it’s a small maintenance concern should preact ever change its internals there</del> ☝︎☝︎☝︎ ref thing above 💯 .

Read more comments on GitHub >

github_iconTop Results From Across the Web

API Reference | Preact
The render function is passed the component's current props and state, and should return a Virtual DOM Element (typically a JSX "element"), an...
Read more >
API Reference | Preact: Fast 3kb React alternative with the ...
The render() function is required for all components. It can inspect the props and state of the component, and should return a Preact...
Read more >
Components | Preact
Components represent the basic building block in Preact. They are fundamental in making it easy to build complex UIs from little building blocks....
Read more >
Components - Preact
Class components are created by extending Preact's Component base class. In the example below, notice how render() takes props as its input and...
Read more >
Web Components | Preact
When rendering static HTML using preact-render-to-string ("SSR"), complex property values like the object above are not automatically serialized. They are ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found