React router not rendering component

Carpetfizz

We are using react-router like so:

ReactDOM.render(
    <Router>
        <Route path="/" component={AnyPic}>
            <Route path="p/:photoId" component={PhotoView} />
        </Route>
    </Router>,
    document.getElementsByClassName("container")[0] 
);

var AnyPic = React.createClass({
    render: function() {
        return (
            <p>Hello world</p>
        )
    }
});

var PhotoView = React.createClass({
    render: function(){
        return (
            <p>This is the photo view</p>
        )
    }
});

After including react-router, what used to be just localhost:8000 started looking like localhost:8000/#/?_k=wulhmi. Not sure where those extra params came from.

Anyway, when trying to access localhost:8000/#/p/XYZ the page keeps going back to /. Any help would be much appreciated.

knowbody

The reason for that is because you are not rendering your children route(s). Check out the react router docs.

If you add this.props.children to your AnyPic component everything will work:

var AnyPic = React.createClass({
    render: function() {
        return (
            <div>
                <p>Hello world</p>
                {this.props.children}
            </div>
        )
    }
});

As @robertklep pointed out in the comment, the "extra" thing in the URL is being added as The Router uses Hash History by default, you probably want to use BrowserHistory to do that you need to install history module: npm install history See the docs here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

React router not rendering component

From Dev

React Router component rendering twice

From Dev

React router dynamic routes not rendering component

From Dev

Redux + React-router component not rendering

From Dev

React Router Switch not rendering specific component

From Dev

React-router sub-path rendering not firing the right component

From Dev

React-Router Switch component not rendering components with same path

From Dev

React Router not rendering page

From Dev

Simple React component not rendering

From Java

Dynamically Rendering a React component

From Dev

Rendering a Component Instance in React

From Dev

React not rendering component

From Dev

Basic react component not rendering

From Dev

Rendering Math in React Component

From Dev

React Router - Nested Routes Not Rendering

From Dev

Redirection with React Router Component

From Dev

React router not displayed a component

From Dev

Controlling Order of React Component Rendering

From Dev

React - server side component rendering

From Dev

React - server side component rendering

From Dev

Component rendering with React in Rails not working

From Dev

Method in React component not rendering HTML

From Dev

Rendering react component in html page

From Dev

Rendering a dynamic child component in React

From Dev

React: Rendering the component in different way

From Dev

React component not rendering on route changed

From Dev

React component not rendering on with latest version

From Dev

React Parent Component Not Rendering Child Component

From Dev

Dynamically Rendering a React Component in React 0.12