Reactjs 使用React将状态值增加1
在React中,我试图使按钮增加存储在状态中的值。 但是,当使用handleClick时,使用下面的代码函数my value设置为undefined或NaNReactjs 使用React将状态值增加1,reactjs,state,Reactjs,State,在React中,我试图使按钮增加存储在状态中的值。 但是,当使用handleClick时,使用下面的代码函数my value设置为undefined或NaN class QuestionList extends React.Component { constructor(props) { super(props); this.state = {value: 0}; // This binding is necessary to make `this` work in
class QuestionList extends React.Component {
constructor(props) {
super(props);
this.state = {value: 0};
// This binding is necessary to make `this` work in the callback
this.handleClick = this.handleClick.bind(this);
}
handleClick = (prevState) => {
this.setState({value: prevState.value + 1});
console.log(this.state.value)
}
你能告诉我为什么会这样吗?根据这里的文档应该是正确的:
设置状态为异步,因此当console.log发生时,您不会看到值更新。您应该在UI上打印状态值,以便查看发生了什么。要修复控制台日志,请尝试以下操作
class QuestionList extends React.Component {
constructor(props) {
super(props);
this.state = {value: 0};
}
handleClick = (prevState) => {
this.setState({value: prevState.value + 1}, () => {
console.log(this.state.value)
});
}
注意:当您为react类定义内联lambda(箭头函数)时,此
已正确绑定,因此不需要在构造函数中绑定它
此外,如果前一个数字只是这样的状态增量,则可以更改传递前一个数字的方式
handleClick = () => {
this.setState({value: this.state.value + 1}, () => {
console.log(this.state.value)
});
}
试试这个
class QuestionList extends React.component {
constructor(props){
super(props)
this.state = {
value : 0
}
}
handleClick(){
this.setState({
value : this.state.value + 1
})
}
render(){
return( <button type="button" onClick={this.handleClick.bind(this)}> {this.state.value} </button> )
}
}
类问题列表扩展了React.component{
建造师(道具){
超级(道具)
此.state={
数值:0
}
}
handleClick(){
这是我的国家({
值:this.state.value+1
})
}
render(){
返回({this.state.value})
}
}
请注意,设置状态时,它会触发渲染函数,该函数将反映当前状态。在浏览器中尝试一下 因为您不正确地使用了handleClick函数。在这里:
handleClick = (prevState) => { .... }
prevState
将是传递给handleClick函数的事件对象,您需要将prevState与setState一起使用,如下所示:
handleClick = () => {
this.setState(prevState => {
return {count: prevState.count + 1}
})
}
另一个问题是,setState是异步的,因此console.log(this.state.value)
不会打印更新的状态值,您需要对setState使用回调函数
检查有关以及如何检查更新值的更多详细信息
检查工作解决方案:
类应用程序扩展了React.Component{
建造师(道具){
超级(道具);
this.state={count:1}
}
onclick(类型){
this.setState(prevState=>{
返回{count:type='add'?prevState.count+1:prevState.count-1}
});
}
render(){
返回(
计数:{this.state.Count}
)
}
}
ReactDOM.render(
,
document.getElementById('容器')
);代码>
类SkuVariantList扩展了React.Component{
建造师(道具){
超级(道具)
此.state={
点击次数:0
};
this.clickHandler=this.clickHandler.bind(this)
}
componentDidMount(){
this.refs.myComponentDiv.addEventListener('click',this.clickHandler);
}
组件将卸载(){
//this.refs.myComponentDiv.removeEventListener('click',this.clickHandler);
}
clickHandler(){
var clk=this.state.clicks
这是我的国家({
点击次数:clk+1
});
}
render(){
让孩子们=this.props.children;
返回(
我的组件({this.state.clicks}clicks})
{this.props.headerText}
{儿童}
);
}
}
您好,请尝试使用这些代码来增加您的价值
class Counter extends React.Component{
constructor(props){
super(props);
this.addOne = this.addOne.bind(this);
this.state = {
count : 0
}
}
addOne() { // addOne as HandleClick
this.setState((preState) => {
return {
count : preState.count + 1
};
});
}
render() {
return (
<div>
<h1>Count : {this.state.count}</h1>
<button onClick={this.addOne}>+1</button>
</div>
);
}
}
ReactDOM.render(<Counter />, document.getElementById('YOUR-ID'));
类计数器扩展React.Component{
建造师(道具){
超级(道具);
this.addOne=this.addOne.bind(this);
此.state={
计数:0
}
}
addOne(){//addOne作为HandleClick
此.setState((预状态)=>{
返回{
计数:preState.count+1
};
});
}
render(){
返回(
计数:{this.state.Count}
+1
);
}
}
ReactDOM.render(,document.getElementById('YOUR-ID');
从“React”导入React
类应用程序扩展了React.Component{
构造函数(){
超级()
此.state={
计数:0
}
this.handleClick=this.handleClick.bind(this)
}
handleClick(){
this.setState(prevState=>{
返回{
计数:prevState.count+1
}
})
}
render(){
返回(
{this.state.count}
改变
)
}
}
导出默认应用程序
这是最短的代码。首先,初始化状态,然后执行递增方法
state = {
counter: 0
}
您也可以这样做,我们使用相同的函数进行递增和递减操作,使其更加模块化和可编辑
class CounterApp extends React.Component{
constructor(){
super();
//here count is initially assigned with 0
this.state ={
count:0
}
}
//when we click Increment or Decrement +1 or -1 is passed to step and the value gets changed it gets updated to the view
increment = (step) =>{
this.setState({
count:this.state.count + step
})
}
render(){
const { count } = this.state;//same as const count = this.state.count;
return(
<div>
<div className="counter-app">
<h2 className="value">{count}</h2>
<button onClick={() => this.increment(+1)}>Increment</button>
<button onClick={() => this.increment(-1)}>Decrement</button>
</div>
</div>
)
}
}
class CounterApp扩展了React.Component{
构造函数(){
超级();
//此处计数最初分配为0
这个州={
计数:0
}
}
//当我们单击递增或递减+1或-1时,它被传递到步骤,并且值被更改,它被更新到视图中
增量=(步长)=>{
这是我的国家({
计数:this.state.count+step
})
}
render(){
const{count}=this.state;//与const count=this.state.count相同;
返回(
{count}
这个.increment(+1)}>increment
此参数为增量(-1)}>减量
)
}
}
谢谢,我不需要使用prevState。@请注意,我已将控制台日志更改为setstate函数的回调。因为内联控制台日志将在线程在setstate函数调用后命中它时执行,而不是在状态更新完成时执行:)我刚刚遇到了这个问题,因为我使用了它,它可以工作。但是,reactJS不喜欢在不使用setState的情况下更新状态。这绝对是最简单的解决方案,但要小心,因为他们可能会在某个时候出错。请注意,您可以使用等待this.setState(…)
,而不是回调。
state = {
counter: 0
}
increaseHandler = () => {
let counter = this.state.counter
counter += 1
this.setState({counter: counter})
}
class CounterApp extends React.Component{
constructor(){
super();
//here count is initially assigned with 0
this.state ={
count:0
}
}
//when we click Increment or Decrement +1 or -1 is passed to step and the value gets changed it gets updated to the view
increment = (step) =>{
this.setState({
count:this.state.count + step
})
}
render(){
const { count } = this.state;//same as const count = this.state.count;
return(
<div>
<div className="counter-app">
<h2 className="value">{count}</h2>
<button onClick={() => this.increment(+1)}>Increment</button>
<button onClick={() => this.increment(-1)}>Decrement</button>
</div>
</div>
)
}
}