我在跟踪路线后没有渲染的组件时遇到了一些问题。路由在父组件Drawings
中创建,并向Drawing
组件发送一些道具。
当我单击链接时,我会找到正确的路径,例如/drawing/20170724
,并且render
函数中的日志语句会运行。我也得到道具,到目前为止一切都很好。但是,返回并不会发生,因此我需要的HTML不可用。
这里是Drawings
,其中创建了路线和链接:
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;
这是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;
我无法弄清楚绘图组件为什么不返回。
答案 0 :(得分:1)
你在做什么是错的。单击链接时更改URL。然后你必须给路由配置,告诉反应路由器在url更改时呈现哪个组件。在index.js
文件中应该是这样的。
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'));
我认为这里的错误是您的路线配置。我从来没有见过像你那样的路线配置。
然后编写一个单独的Drawing
组件,该组件呈现给定的drawing
实例。
通过此更改,您的链接应该是这样的。
<Link to={`/drawing/${drawing.date}`}>
{drawing.date}
</Link>
底线是这个。在这里你给一些带有超链接的文字。点击此路由器后,立即更改网址。然后它使用router config来呈现相关组件。
希望这会有所帮助。快乐的编码!