[英]react router link to, inside map function, works only one time
I'have followed this react router example , but when I click a second time on another user, it seems that the User component state still unchanged.我已经遵循了这个react router example ,但是当我第二次单击另一个用户时,用户组件状态似乎仍然没有改变。 So, I can see just one user.所以,我只能看到一个用户。
package.json包.json
{
"name": "raspberry",
"version": "1.0.0",
"description": "raspberry =========",
"main": "index.js",
"scripts": {
"start": "node webpack.dev-server.js",
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"react": "^0.14.7",
"react-bootstrap": "^0.30.7",
"react-dom": "^0.14.7",
"react-router": "^2.0.0",
"superagent":"^3.3.2"
},
"devDependencies": {
"babel-core": "^6.21.0",
"babel-loader": "^6.2.10",
"babel-preset-es2015": "^6.18.0",
"babel-preset-react": "^6.16.0",
"bootstrap-sass": "^3.3.7",
"css-loader": "^0.26.1",
"extract-text-webpack-plugin": "^1.0.1",
"file-loader": "^0.9.0",
"node-sass": "^4.3.0",
"react-bootstrap": "^0.30.7",
"react-hot-loader": "^1.3.0",
"sass-loader": "^4.1.1",
"style-loader": "^0.13.1",
"webpack": "^1.14.0",
"webpack-dev-server": "^1.16.2"
}
}
app.js应用程序.js
ReactDom.render((
<Router history={hashHistory}>
<Route path="/" component={Layout}>
<IndexRoute component={Home} />
<Route path="/home" component={Home}/>
<Route path="/users" component={Users}>
<Route path="/users/:userId" component={User}/>
</Route>
<Route path="*" component={NoMatch} />
</Route>
</Router>
), document.getElementById('app'));
Users.js用户.js
const Users = React.createClass({
getUsers: function() {
var self = this;
request
.get('/api/users')
.set('Auth', 'Bearer '+ this.state.token)
.end(function(err, res){
if (res.statusCode === 200){
self.setState({
users:JSON.parse(res.text)
}) ;
}else{
console.log('Get Users failed');
}
});
},
getInitialState: function(props) {
props = props || this.props;
return {
token : localStorage.getItem("token"),
users:[]
};
},
componentDidMount: function() {
this.getUsers();
},
render: function() {
return(
<div>
<h1>Users</h1>
<div className="master">
<div className="list-group">
{this.state.users.map(user => (
<Link to={`/users/${user.id}`} key={user.id} className="list-group-item">{user.username}</Link>
))}
</div>
</div>
<div className="detail">
{ this.props.children }
</div>
</div>
);
}
});
export default Users;
User.js用户.js
const User = React.createClass({
findUserById: function(id){
var self = this;
request
.get('/api/users/'+id)
.set('Auth', 'Bearer '+ localStorage.getItem("token"))
.end(function(err, res){
console.log(res);
if (res.statusCode === 200){
self.setState({
user:JSON.parse(res.text)
}) ;
}else{
console.log('Get User failed');
}
});
},
getInitialState: function() {
return {user:''};
},
componentDidMount: function() {
console.log('componentDidMount');
this.findUserById(this.props.params.userId)
},
render:function() {
return (
<div>
<h2>{this.state.user.username}</h2>
</div>
)
}
});
export default User;
If you are at the location /users/17
, you will be rendering the components <Layout>
, <Users>
, and <User>
. 如果位于/users/17
位置,则将呈现组件<Layout>
, <Users>
和<User>
。 If you then navigate to the location /users/5
, you will still be rendering the same components. 如果您随后导航到/users/5
位置,则仍将渲染相同的组件。 That means that in order for <User>
to render based on the new params
, you need to implement a componentWillReceiveProps
or componentDidUpdate
method on that component. 这意味着为了使<User>
基于新的params
进行呈现,您需要在该组件上实现componentWillReceiveProps
或componentDidUpdate
方法。
How to actually implement componentWillReceiveProps or componentDidUpdate method on that component?如何在该组件上实际实现 componentWillReceiveProps 或 componentDidUpdate 方法? What to do when using react hooks?使用 React 钩子时该怎么办?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.