reactjs - 尽管使用了 withRouter,为什么 this.props.history 未定义?
问题描述
我正在尝试this.props.history.push...
在我的组件中执行此操作,但即使在确保使用它导出它之后,withRouter
我仍然会收到此错误:
Uncaught TypeError: Cannot read property 'push' of undefined
我还确保使用它的父组件也包含在 aProtectedRoute
中:
// 我的组件:
import React from 'react';
import { withRouter } from 'react-router-dom';
import { Link } from 'react-router-dom';
class UserIndexItem extends React.Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
this.play = this.play.bind(this);
}
handleClick(e) {
if (!e.target.classList.contains("triangle")) {
this.props.history.push(`/playlist/${this.props.playlist.id}`);
}
}
handleTrack(playlist) {
// still going forward one, then back one, and then it plays normally...
if (!playlist.payload.tracks) return;
let tracks = Object.values(playlist.payload.tracks);
let currentTrack = tracks[0];
let nextTrack = tracks[1];
this.props.receiveCurrentTrack(currentTrack);
this.props.receiveNextTrack(nextTrack);
this.props.receiveTitle(currentTrack.title);
this.props.receiveArtist(currentTrack.artist);
this.props.receiveAlbumId(currentTrack.album_id);
}
play() {
const { playlist } = this.props;
this.props.requestSinglePlaylist(this.props.playlist.id).then(playlist => this.handleTrack(playlist));
this.props.receivePlaylistId(playlist.id);
}
render() {
const { playlist } = this.props;
return (
<li>
<div className="playlist-image" onClick={ this.handleClick }>
<div className="play-button" onClick={ this.play }>
<div className="triangle right"></div>
<div className="circle"></div>
</div>
<div className="overlay"></div>
<img src={playlist.photo_url} alt="Playlist thumbnail" onClick={ this.handleClick }/>
</div>
<div className="playlist-name">
<Link to={`/playlist/${playlist.id}`}>{ playlist.title}</Link>
</div>
</li>
);
}
}
export default withRouter(UserIndexItem);
// 我的父组件:
import React from 'react';
import UserIndexItem from './user_index_item';
import { selectTracksFromPlaylist } from '../../reducers/selectors';
class UserIndex extends React.Component {
constructor(props) {
super(props);
}
render() {
const { user, playlists } = this.props;
return(
<div className="user-index-container">
<div className="header">
<h1>{ user.username }</h1>
<h2>Public Playlists</h2>
</div>
<div className="playlists">
<ul>
{ playlists.map(playlist =>
<UserIndexItem
key={ playlist.id }
playlist={ playlist }
requestSinglePlaylist={ this.props.requestSinglePlaylist }
receiveCurrentTrack={ this.props.receiveCurrentTrack }
receiveNextTrack = { this.props.receiveNextTrack }
receiveTitle={ this.props.receiveTitle }
receiveArtist={ this.props.receiveArtist }
receivePlaylistId={ this.props.receivePlaylistId }
receiveAlbumId={ this.props.receiveAlbumId }
/>)
}
</ul>
</div>
</div>
);
}
}
export default UserIndex;
// 我使用父组件的路由:
<ProtectedRoute path="/users/:userId" component={UserIndex} />
// 我的 ProtectedRoute 实现:
const Protected = ({ component: Component, path, loggedIn, exact }) => (
<Route path={ path } exact={ exact } render={ (props) => (
loggedIn ? (
<Component {...props} />
) : (
<Redirect to="/welcome" />
)
) }/>
);
解决方案
你可以这样尝试:
<ProtectedRoute path="/users/:userId" component={props => <UserIndex {...props} />} />
请让我知道这是否有效。
谢谢。
推荐阅读
- javascript - 在 DataTables 中对日期进行排序?(功能覆盖)
- node.js - 无法使用 axios 在 android 7.0 设备中从/向本地主机服务器获取/发布数据 - React Native 应用程序
- javascript - CSS 菜单悬停下划线动画
- javascript - 测试跟踪最后发出的值的 RxJS observable(没有完整的信号)?
- css - 浮动标签和占位符重叠
- lua - 使用 WxLua 需要安装 miniZip
- attributes - 在 Magento 管理员中保存产品属性时找不到页面
- graphics - 使用 Directx11 的绘图框但不显示
- python - 如何使用 pandas 有效地执行逐行操作?
- c++ - 规则“用户定义但无操作的析构函数也是非平凡析构函数”是否过于严格?