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.

Question: Rendering (inserting) before instead of appending

See original GitHub issue

Maybe I’ve missed something. If render is the proper way to generate code with Preact, I can’t seem to figure out the proper way to generate and insert code out-of-order. What I mean: render appends.

For example:

render(<div>Post 1: Hello World</div>, document.body);
render(<div>Post 2: Blah Blah</div>, document.body);

Which outputs:

<div>Post 1: Hello World</div>
<div>Post 2: Blah Blah</div>

I want this instead:

<div>Post 2: Blah Blah</div>
<div>Post 1: Hello World</div>

Other than the obvious (switching the order), or inserting proxy elements that I target instead, what should I be doing in the 2nd line so it goes before the 1st element; Or specifically, so it inserts at the top of document.body instead of the bottom?

Thanks.

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
developitcommented, Aug 14, 2016

You can definitely re-render using render(), just you lose a lot of the benefits of component-based design. It’s also mildly confusing because there is render() (import { render } from 'preact') and there is also a render() method on components.

Here’s a little more fully-featured example continuing from above:

import { h, Component, render } from 'preact';

class App extends Component {
  state = {
    posts: []   // we'll keep all received posts in an array
  };

  // when App is mounted, start polling for updates
  componentDidMount() {
    this.getUpdates();
  }

  // fetches new posts, then calls itself (to poll)
  getUpdates = () => {
    fetch('/updates')
      .then( r => r.json() )
      .then( newPosts => {
        // grab existing posts:
        let { posts } = this.state;

        // append new ones:
        posts = posts.concat(newPosts);

        // save updated posts to state (this automatically re-renders):
        this.setState({ posts });

        // wait a sec before polling again:
        setTimeout(this.getUpdates, 1000);
      });
  }

  // render gets called whenever state changes.
  // The returned JSX gets intelligently diffed against the dom.
  render(props, state) {
    // we can reverse-sort posts as part of the UI rendering:
    let posts = state.posts.slice().reverse();
    return (
      <div>
        { posts.map( post => (
          <div>
            {post.title}: {post.body}
          </div>
        )) }
      </div>
    );
  }
}

// render and mount App. This kicks everything off.
render(<App />, document.body);

One thing worth noting: React/preact/etc actually simplify this kind of UI nicely, but it takes a little while to get used to the different way of doing things. Whereas with jquery/etc you might have to know “I want to insert new posts above existing posts”, with this component model, you only have to tell it how to render all posts (whether they are new or old). This way, determining whether new posts get injected first or last is simply a matter of choosing where to put them in an Array.

1reaction
developitcommented, Aug 14, 2016

Happy to help 😃 Hopefully I’ll have some time to put better documentation together - I’d like to do some tutorials and such, perhaps on EggHead.io.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reactjs : Append instead of replacing with render method
that should work fine. but i would append it before rendering. · Can you explain your situation where you would want to do...
Read more >
RTE Inserting/Rendering Link Appending Text
I am looking for the best way to append to the text of a RTE media link (PDF file type). Either on the...
Read more >
How to Render in SketchUp (Answers to the 3 ... - YouTube
Want to learn photorealistic rendering ? This video answers the 3 questions everyone asks when struggling to get started.
Read more >
Node.appendChild() - Web APIs - MDN Web Docs
cloneNode() method can be used to make a copy of the node before appending it under the new parent. Copies made with cloneNode...
Read more >
Custom Render of Survey Elements - SurveyJS
Custom Render of Survey Elements | JS Survey and Form Library Example for React. ... Show Preview before complete ... Lazy questions rendering....
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