React-router-dom 重定向问题
React-router-dom redirect issue
我正在做这个项目,它是 youtube 的一个实现,比方说我搜索 'Sia' 例如在“/”我得到的结果包括视频、频道、播放列表,当我点击我使用频道组件路由到“/channel”的频道项目现在问题是,当我在 /channel 搜索某些内容时,我应该重定向回“/”并使用提交的搜索词获得搜索结果。但我不知道出了什么问题,或者让 Header 组件成为 BrowserRouter 的直接子组件或将它连同它的道具一起呈现在每个路由组件中是否是个好主意(无论如何我都这样做了)
这是通道组件和路由
class ChannelDisplay extends React.Component {
onFormSubmit = (term) => {
this.props.fetchList(term);
this.props.defaultVideo(term);
}
renderHeader() {
const {channel} = this.props
if(!channel.snippet) return <Search/>
if(channel) {
const subNum = `${Number(channel.statistics.subscriberCount).toLocaleString()}`
return (
<div className="channel">
<Header onFormSubmit={this.onFormSubmit}/>
<div className="container">
<img className="img-fluid" src={channel.brandingSettings.image.bannerImageUrl} alt={channel.snippet.title} />
<div className="d-flex flex-nowrap">
<img className="img-thumbnail img-fluid channel-img mx-2 my-2" src={channel.snippet.thumbnails.default.url} alt={channel.snippet.title} />
<div className="media-content">
<p>{channel.snippet.title}</p>
<span><i className="fab fa-youtube mr-2"></i> Subscribe {subNum}</span>
</div>
</div>
</div>
</div>
)
}
}
render() {
return this.renderHeader()
}
}
const mapStateToProps = state => {
return {channel:state.channel}
}
export default connect(mapStateToProps,{fetchList,defaultVideo})
(ChannelDisplay)
.
render() {
return (
<div>
<BrowserRouter>
<div>
<Route path="" exact component={Search} />
<Route path="/channel" exact component={ChannelDisplay} />
</div>
</BrowserRouter>
</div>
)
}
也许您应该将 history.push
或 history.replace
添加到 Search.js
文件中的提交函数中,但我认为推送是更好的选择,因为您将能够返回back button
到您的频道或视频或其他内容。
onFormSubmit = (term) => {
this.props.fetchList(term);
this.props.defaultVideo(term);
this.props.history.push('/');
};
我正在做这个项目,它是 youtube 的一个实现,比方说我搜索 'Sia' 例如在“/”我得到的结果包括视频、频道、播放列表,当我点击我使用频道组件路由到“/channel”的频道项目现在问题是,当我在 /channel 搜索某些内容时,我应该重定向回“/”并使用提交的搜索词获得搜索结果。但我不知道出了什么问题,或者让 Header 组件成为 BrowserRouter 的直接子组件或将它连同它的道具一起呈现在每个路由组件中是否是个好主意(无论如何我都这样做了) 这是通道组件和路由
class ChannelDisplay extends React.Component {
onFormSubmit = (term) => {
this.props.fetchList(term);
this.props.defaultVideo(term);
}
renderHeader() {
const {channel} = this.props
if(!channel.snippet) return <Search/>
if(channel) {
const subNum = `${Number(channel.statistics.subscriberCount).toLocaleString()}`
return (
<div className="channel">
<Header onFormSubmit={this.onFormSubmit}/>
<div className="container">
<img className="img-fluid" src={channel.brandingSettings.image.bannerImageUrl} alt={channel.snippet.title} />
<div className="d-flex flex-nowrap">
<img className="img-thumbnail img-fluid channel-img mx-2 my-2" src={channel.snippet.thumbnails.default.url} alt={channel.snippet.title} />
<div className="media-content">
<p>{channel.snippet.title}</p>
<span><i className="fab fa-youtube mr-2"></i> Subscribe {subNum}</span>
</div>
</div>
</div>
</div>
)
}
}
render() {
return this.renderHeader()
}
}
const mapStateToProps = state => {
return {channel:state.channel}
}
export default connect(mapStateToProps,{fetchList,defaultVideo})
(ChannelDisplay)
.
render() {
return (
<div>
<BrowserRouter>
<div>
<Route path="" exact component={Search} />
<Route path="/channel" exact component={ChannelDisplay} />
</div>
</BrowserRouter>
</div>
)
}
也许您应该将 history.push
或 history.replace
添加到 Search.js
文件中的提交函数中,但我认为推送是更好的选择,因为您将能够返回back button
到您的频道或视频或其他内容。
onFormSubmit = (term) => {
this.props.fetchList(term);
this.props.defaultVideo(term);
this.props.history.push('/');
};