<small id='BccLB'></small><noframes id='BccLB'>

<tfoot id='BccLB'></tfoot>
<legend id='BccLB'><style id='BccLB'><dir id='BccLB'><q id='BccLB'></q></dir></style></legend>
    • <bdo id='BccLB'></bdo><ul id='BccLB'></ul>

      1. <i id='BccLB'><tr id='BccLB'><dt id='BccLB'><q id='BccLB'><span id='BccLB'><b id='BccLB'><form id='BccLB'><ins id='BccLB'></ins><ul id='BccLB'></ul><sub id='BccLB'></sub></form><legend id='BccLB'></legend><bdo id='BccLB'><pre id='BccLB'><center id='BccLB'></center></pre></bdo></b><th id='BccLB'></th></span></q></dt></tr></i><div id='BccLB'><tfoot id='BccLB'></tfoot><dl id='BccLB'><fieldset id='BccLB'></fieldset></dl></div>
      2. 像按钮一样反应切换

        时间:2023-09-06
              <legend id='1oVOs'><style id='1oVOs'><dir id='1oVOs'><q id='1oVOs'></q></dir></style></legend>

              <small id='1oVOs'></small><noframes id='1oVOs'>

                <bdo id='1oVOs'></bdo><ul id='1oVOs'></ul>
              • <i id='1oVOs'><tr id='1oVOs'><dt id='1oVOs'><q id='1oVOs'><span id='1oVOs'><b id='1oVOs'><form id='1oVOs'><ins id='1oVOs'></ins><ul id='1oVOs'></ul><sub id='1oVOs'></sub></form><legend id='1oVOs'></legend><bdo id='1oVOs'><pre id='1oVOs'><center id='1oVOs'></center></pre></bdo></b><th id='1oVOs'></th></span></q></dt></tr></i><div id='1oVOs'><tfoot id='1oVOs'></tfoot><dl id='1oVOs'><fieldset id='1oVOs'></fieldset></dl></div>

                  <tfoot id='1oVOs'></tfoot>
                    <tbody id='1oVOs'></tbody>
                  本文介绍了像按钮一样反应切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个状态为:{likes: 123} 的组件我在喜欢"按钮旁边显示喜欢的数量.我如何为这个按钮实现一个功能,所以当我单击它一次时,它会添加到喜欢(所以 state.likes = 124),然后如果我想第二次单击它,它会返回到以前的状态(state.likes= 123).当然,它始终显示正确的点赞数.到目前为止,这是我所得到的:

                  class ProfileInfo extends React.Component {构造函数(道具){超级(道具);这个.state = {喜欢:this.props.likes,};}句柄 = () =>{this.setState(prevState => ({喜欢:prevState.likes + 1,}));}使成为() {返回 (

                  <button className="like-button" onClick={this.handleLike}>喜欢</按钮></div>);}}导出默认 ProfileInfo;

                  它只是不断地添加喜欢.

                  解决方案

                  您可以执行以下操作.

                  这里是一个codesandbox,其中包含更完整的代码版本.

                  从'react'导入反应;类喜欢扩展 React.Component {构造函数(道具){超级(道具);这个.state = {喜欢:124,更新:假};}更新喜欢 = () =>{如果(!this.state.updated){this.setState((prevState, props) => {返回 {喜欢:prevState.likes + 1,更新:真};});} 别的 {this.setState((prevState, props) => {返回 {喜欢:prevState.likes - 1,更新:假};});}}使成为(){返回(

                  <p onClick={this.updateLikes}>点赞</p><p>{this.state.likes}</p></div>);}}导出默认点赞;

                  I have a component with a state: {likes: 123} I display the number of likes next to a 'like' button. How do I implement a functionality to this button, so when I click it once, it adds to likes (so state.likes = 124), then if I want to click it a second time it goes back to previous state (state.likes = 123). Of course, it displays the correct number of likes at all times. Here's what I've got so far:

                  class ProfileInfo extends React.Component {
                    constructor(props) {
                      super(props);
                      this.state = {
                        likes: this.props.likes,
                      };
                    }
                    handleLike = () => {
                      this.setState(prevState => ({
                        likes: prevState.likes + 1,
                      }));
                    }
                    render() {
                      return (
                        <div>
                          <button className="like-button" onClick={this.handleLike}>
                            Like
                          </button>
                        </div>
                      );
                    }
                  }
                  
                  export default ProfileInfo;

                  It just adds likes on and on.

                  解决方案

                  You could do something like the following.

                  Here is a codesandbox with a more complete version of the code.

                  import React from 'react';
                  
                  class Likes extends React.Component {
                  
                    constructor(props){
                  
                      super(props);
                      this.state = {
                        likes: 124,
                        updated: false
                      };
                  
                    }
                  
                    updateLikes = () => {
                  
                      if(!this.state.updated) {
                        this.setState((prevState, props) => {
                          return {
                            likes: prevState.likes + 1,
                            updated: true
                          };
                        });
                  
                      } else {
                  
                        this.setState((prevState, props) => {
                          return {
                            likes: prevState.likes - 1,
                            updated: false
                          };
                        });
                  
                      }
                    }
                  
                    render(){
                  
                      return(
                        <div>
                          <p onClick={this.updateLikes}>Like</p>
                          <p>{this.state.likes}</p>
                        </div>
                      );
                    }
                  }
                  
                  export default Likes;
                  

                  这篇关于像按钮一样反应切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:我可以根据字母数更改字体大小吗? 下一篇:为什么 React Fibre 会多次调用 componentWillMount?

                  相关文章

                    <bdo id='YUSk0'></bdo><ul id='YUSk0'></ul>
                  <legend id='YUSk0'><style id='YUSk0'><dir id='YUSk0'><q id='YUSk0'></q></dir></style></legend>

                  <tfoot id='YUSk0'></tfoot>
                  1. <small id='YUSk0'></small><noframes id='YUSk0'>

                  2. <i id='YUSk0'><tr id='YUSk0'><dt id='YUSk0'><q id='YUSk0'><span id='YUSk0'><b id='YUSk0'><form id='YUSk0'><ins id='YUSk0'></ins><ul id='YUSk0'></ul><sub id='YUSk0'></sub></form><legend id='YUSk0'></legend><bdo id='YUSk0'><pre id='YUSk0'><center id='YUSk0'></center></pre></bdo></b><th id='YUSk0'></th></span></q></dt></tr></i><div id='YUSk0'><tfoot id='YUSk0'></tfoot><dl id='YUSk0'><fieldset id='YUSk0'></fieldset></dl></div>