【发布时间】:2018-01-22 22:40:03
【问题描述】:
我有一个问题,我尝试在网上查找,但找不到我需要的内容,但这可能是因为我无法正确表达我的问题。
我正在整理一个 React 项目,我想为人们创建用户个人资料页面。这是我目前所拥有的。
<Route path="/profile/:user" render={(routeProps) => (<Profile {...routeProps} {...this.state} /> )}/>
这是在我的 index.js 中,用于为不同用户设置我的个人资料页面的路线,以下是我的个人资料页面的路线
import React from 'react';
import ReactDOM from 'react-dom';
import { NavLink } from 'react-router-dom';
export class Profile extends React.Component{
constructor(props){
super(props);
this.state={
user: this.props.user,
fullname: this.props.fullname,
picture: this.props.picture
};
}
render(){
return(
<h1>{this.state.fullname}</h1>
);
}
}
现在我的问题是这个。如果 URL 与状态中的“用户”给出的用户 ID 匹配,我希望个人资料页面仅加载和呈现用户全名
在我的 index.js 中,我硬编码了用户和全名值来测试它,它们是这样设置的
constructor(props){
super(props);
this.state = {
user:"AFC12345",
fullname:"Bob Ross"
};
我只想在我访问“http://localhost:8080/#/Profile/AFC12345”时呈现个人资料页面,目前它会为我访问的任何“个人资料/xxxx”呈现个人资料页面。
【问题讨论】:
标签: reactjs react-router