简体   繁体   中英

React component not rendering with React Router

I am having some trouble with a component not rendering after following its route. The routes are created in a parent component Drawings , and send a couple of props to Drawing components.

When I click the link, I get to the correct path, for example, /drawing/20170724 , and the log statement I have in the render function runs. I also get the props, so far so good. However, the return doesn't happen, so the HTML I need isn't available.

Here is Drawings where the routes and links are created:

import React, { Component } from "react";
import { Route, Link } from "react-router-dom";
import AnimatedWrapper from "../modules/AnimatedWrapper";

import Drawing from '../components/Drawing';

class DrawingsComponent extends Component {
  render() {
    const drawings = this.props.drawings;
    const drawingsMap = this.props.drawingsMap;

    return(
      <div>
        <div className="page">
          <div className="drawings-list">
            {
              drawings.map((drawing) => {
               return(
                <Link to={`/drawing/${drawing.date}`} key={drawing.date}>
                  <div className="drawing-thumb">
                    <h2>{drawing.date}</h2>
                  </div>
                </Link>
                )
              })
            }
          </div>
          {
          Object.keys(drawingsMap).map((d, i) => {
            return <Route path={`/drawing/${drawings[i].date}`} render={(props) => (<Drawing drawingPkg={drawingsMap[d]} drawingInfo={drawings[i]} {...props} />)} key={`${drawings[i].date}`}/>
          })}
        </div>
      </div>
    )
  }
}

const Drawings = AnimatedWrapper(DrawingsComponent);
export default Drawings;

And here is Drawing :

import React, { Component } from 'react';
import AnimatedWrapper from "../modules/AnimatedWrapper";

class DrawingComponent extends Component {
    render() {
        const drawing = this.props.drawingPkg;
        const drawingInfo = this.props.drawingInfo;

        console.log('gonna draw now');

        return(
            <div className="drawing">
                <h2 className="drawing-title">{drawingInfo.title}</h2>
                <canvas></canvas>
            </div>
        )
    }
}

const Drawing = AnimatedWrapper(DrawingComponent);
export default Drawing;

I can't figure out why the Drawing component isn't returning.

What you are doing is wrong. You change the URL when the link is clicked. Then you have to give routes configuration, that tells react router which component to render when the URL changes. It should be something like this inside your index.js file.

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import promise from 'redux-promise';

import reducers from './reducers';
import Drawing './components/drawing_component';


const createStoreWithMiddleware = applyMiddleware(promise)(createStore);

ReactDOM.render(
  <Provider store={createStoreWithMiddleware(reducers)}>
    <BrowserRouter>
      <div>
        <Switch>
            <Route path="/drawings/:date" component={Drawing} />
            <Route path="/" component={IndexComponent} />
        </Switch>
      </div>
    </BrowserRouter>
  </Provider>
  , document.querySelector('.container'));

I think what is wrong here is your routes config. I have never seen route config is given like the way you do.

Then write a separate Drawing component which renders a given drawing instance.

With this change your Link should be like this.

<Link to={`/drawing/${drawing.date}`}>
  {drawing.date}
</Link>

The bottom line is this. Here you give some text with a hyperlink. Upon clicking this router changes the URL as in your case now. Then it uses router config to render the relevant component.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM