class Signup extends React.Component {
constructor() {
super();
this.state = { count: 0 }
}
hndlChange() {
//here it's okay, It increasing count value
this.setState({ count: this.state.count + 1 });
//but here it printing previous value
console.log(this.state.count);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在调用这个函数。因为它正在增加count价值,但是当我访问它时,它会返回以前的值。我想在同一个函数中访问最新的更新值,我应该怎么做。
如何在同一函数中访问最新的状态值
我创建了下一个 js 应用程序。
现在我正在尝试将它部署在 AWS ec2 实例上。我可以像在我的开发服务器(本地服务器)上一样轻松地将它部署在 ec2 实例上(我将所有代码库移动到 ec2 并运行 npm install、npm run build 和 npm run start)。
但问题是,在我们在我的应用程序上部署任何更新(添加新功能)之后,假设如果我遵循与上述相同的过程,在这种情况下,我需要停止节点服务器,但我不想阻止它。
所以请给我一个替代方案,以便它可以轻松部署并更新我的应用程序。
import React, { Component } from 'react';
class one extends React.Component
{
constructor()
{
super();
this.state = {
number:26
}
}
render()
{
return(
<div></div>
);
}
}
export default one;
import React, { Component } from 'react';
import one from './one'
class HomePage extends React.Component
{
render()
{
return(
<div>{one.state.number}</div>
);
}
}
export default HomePage;
Run Code Online (Sandbox Code Playgroud)
是否有可能访问数字状态? 有没有办法将一个组件的状态访问到另一个组件?如果有解决方案,请提出建议。