reactjs - React Not Found 渲染服务器端
问题描述
我使用React Router v4,效果很好。
<Switch>
<Router path="/users/:uid" component={Profile} />
<Router path="*" component={NotFound} />
</Switch>
好的,如果我访问域/blabla
NotFound组件渲染得很好。
但我想知道当用户不存在时如何渲染NotFound 。/users/:uid
app.get('/users/:uid', (req, res) {
if( // user exist) {
res.json({user})
}
else res.status(404).json({ message: 'User not exist'})
})
也许我需要连接服务器响应,但我不知道什么是最好的方法。
class Profile extends Component {
componentWillMount = () => {
this.props.fetchUser(uid)
}
render(){
if(!this.props.loading && !this.props.user) return <NotFound />
else return (...)
}
}
这是正确的方法吗?我一起使用redux。
解决方案
class Profile extends Component {
render(){
if(!this.props.loading && !this.props.user) {
this.props.history.push('/bababa') // you can jump to notFound
return
}
else return (...)
}
}
这是?
推荐阅读
- python-3.x - 如何在 Scikit-Learn 中获取 GridSearchCV() 的 OneVsRestClassifier(LinearSVC()) 的估计器键引用?
- reactjs - 失败的道具类型:ForwardRef(Button):道具类型`component`无效:React中带有材料ui的警告
- c# - 在 Windows 窗体属性中调整大小不起作用
- android - 我是否必须为打捞的 android 地图应用程序重新生成新的 SHA-1 密钥?
- php - 无法在构造函数中获取会话
- python-3.x - 您如何限制多个数字的输出中的小数位数?
- python - 读取并打印“STDIN 和 STDOUT”
- sql - 运行宏(特别是电子邮件)时如何更新字段(特别是日期)
- r - 使用 Reactive Directory 的 R 功能太慢
- java - React 和 Spring Boot 的 Oath2 实现