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.

Render Not Displaying HTML

See original GitHub issue

I added a Component that changes the page depending on what is in the view property like the one below. When updateView is called it changes what is in the view.

import {
    h,
    render,
    Component
} from 'preact';
import Home from './home';
import Status from './Status';

let view;
class Pages extends Component {
    constructor() {
        super();
        view = <Status />
    }
    updateView(redirect, state) {
        switch (redirect) {
            case "HOME":
                view = <Home state={state} />
                break;
                }
    getView() {
        return view;
    }
    reset() {
        view = <Status />;
    }
}
const PagesInstance = new Pages();
export default PagesInstance;

The below class is updated and it shows whatever is in in the view.

import {
    h,
    render,
    Component
} from 'preact';
import objectAssign from 'object-assign';
import Login from './login';
let viewReference = null;
export default class AuthenticatedRoute extends Component {
    constructor() {
        super();
        this.onChange = this.onChange.bind(this);
        this.setState(AuthStore.getState());
    }
    componentDidMount() {
        AuthStore.addChangeListener(this.onChange);
        UserStore.addChangeListener(this.onChange);
    }
    componentWillUnmount() {
        AuthStore.removeChangeListener(this.onChange);
        UserStore.removeChangeListener(this.onChange);
    }
    onChange() {
        this.setState(AuthStore.getState());
    }
    render() {
        if (!AuthStore.getState().loggedIn) {
            return <Login />;
        } else {
            if (UserStore.getView() != viewReference) { 
                viewReference = objectAssign({}, UserStore.getView());
                return UserStore.getView();
            }
        }
    }
}

The issue is that even though most of the other Components display, when someone log out the <Login /> rendered JSX or even just a div doesn’t display. At the beginning it work just fine. The console even logs that it is constructed and rendered after the update has occured, but nothing shows and there is not a div. Can you give an any reason why this is happening? Below are the rest of the classes just in case they may be useful.

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

export default class Login extends Component {
    constructor() {
        super();
        console.log("Login Constructed");
       }
    render() {
        console.log("Login Rendered");
        return <div>
                LOGIN
            </div>
    }
}

The UserStore only gets the view from the page component.

import Pages from '../pages';
class UserStore extends FluxStore {
    constructor() {
        super();
    }
    getView() {
        return Pages.getView();
    }
}

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
developitcommented, Jun 1, 2016

You can manually specify a URL for preact-router, which makes it ignore the browser’s URL. That “url” can actually just be a component name.

However, if you just want to conditionally render components based on a view or some state (like “is logged in”), you could just use conditionals:

class Views extends Component {
  state = {
    view: this.props.view   // can pass default view in as a prop
  };

  // route to a given view
  route = view => {
    this.setState({ view });
  };

  // child components can call:  this.context.route('some-view');
  getChildContext() {
    return { route: this.route };
  }

  render({ children }, { view }) {
    // just render the child whose `name` prop matches the current view:
    return children.filter( child => child.attributes.name===view )[0] || null;
  }
}

// Trick: for logged-out, skip the router/views entirely:
class App extends Component {
  // initialize our state to the auth store's state:
  state = AuthStore.getState();

  onChange = () => {
    this.setState(AuthStore.getState());
  };

  componentDidMount() {
    AuthStore.addChangeListener(this.onChange);
  }

  componentWillUnmount() {
    AuthStore.removeChangeListener(this.onChange);
  }

  render(props, state) {
    // if logged out, always force the login view:
    if (!state.loggedIn) {
      return <Login />;
    }
    return (
      <Views view="home">
        <Home name="home" />
        <Other name="other" />
      </Views>
    );
  }
}

class Home extends Component {
  // switch to the "Other" view
  goOther = () => {
    this.context.route('other');
  };
  render() {
    return (
      <div>
        <a onClick={this.goOther}>Other</a>
      </div>
    );
  }
}

Here’s a working demo of the above setup (I just mocked out your Flux store for the demo): https://jsfiddle.net/developit/k487h3t5/

preview


Edit: It’s probably worth noting that you can wrap up the routing stuff into a <Link /> component much like preact-router did/does:

/** <Link to="home">Home</Link> */
const Link = ({ to, ...props }, context) => (
  <a onClick={ () => context.route(to) } {...props} />
);
0reactions
developitcommented, Jun 2, 2016

I’m going to close this one out since we solved the original question, but please feel free to chat 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why isn't html rendering on my web browser? - Stack Overflow
Show activity on this post. It should be rendered by the browser, not the webserver. Check the HTTP headers "Content-Type". It should read ......
Read more >
Why are HTML documents not rendering and just shown ... - IBM
After upgrading BPM, my HTML documents that were once rendering are no longer being displayed correctly and are being shown as plain text....
Read more >
Html Render not working - Atlassian Community
Solved: Hello. I am wondering if you can help with this question. Basically we have a single select CustomField called RAG Status. The...
Read more >
Layouts and Rendering in Rails - Ruby on Rails Guides
Layouts and Rendering in Rails. This guide covers the basic layout features of Action Controller and Action View. After reading this guide, you...
Read more >
[Bug] Not rendering properly · Issue #456 - GitHub
Bug Report. HTML is not being rendered properly. ... However, we try to follow the HTML and CSS standards as the default 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