简体   繁体   English

为什么输入在 React 中不返回任何内容?

[英]Why is input not returning anything in React?

I'm a beginner in React, and I'm trying to make small apps with it.我是 React 的初学者,我正在尝试用它制作小应用程序。 In fact, this is my first app which I'm making in React, and I'm stuck in a problem.事实上,这是我在 React 中制作的第一个应用程序,但我遇到了一个问题。

I'm making a small app that will take in numbers and add, subtract, or multiply them, ie a calculator.我正在制作一个小型应用程序,可以输入数字并对其进行加、减或乘运算,即计算器。 Here is the code.这是代码。

 const { Component } = React; class App extends React.Component { state = { num1: 0, num2: 0, answer: 0, equation: "", }; handleAdd1 = (number) => { this.setState({ num1: number }); }; handleAdd2 = (number2) => { this.setState({ num2: number2 }); }; handleClear = () => { this.setState({ num1: 0, num2: 0, answer: 0 }); }; handlePlus = (sign) => { switch (sign) { case "+": this.setState({ answer: this.state.num1 + this.state.num2 }); break; case "-": this.setState({ answer: this.state.num1 - this.state.num2 }); break; case "x": this.setState({ answer: this.state.num1 * this.state.num2 }); break; } }; handleInput = (event) => { this.setState({ equation: event.target.value }); }; render() { return ( <div> <div> <h1>{this.state.num1}</h1> <button onClick={() => this.handleAdd1(1)}> 1 </button> <button onClick={() => this.handleAdd1(2)}> 2 </button> <button onClick={() => this.handleAdd1(3)}> 3 </button> <button onClick={() => this.handleAdd1(4)}> 4 </button> <button onClick={() => this.handleAdd1(5)}> 5 </button> <button onClick={() => this.handleAdd1(6)}> 6 </button> <button onClick={() => this.handleAdd1(7)}> 7 </button> <button onClick={() => this.handleAdd1(8)}> 8 </button> <button onClick={() => this.handleAdd1(9)}> 9 </button> <button onClick={() => this.handleAdd1(0)}> 0 </button> </div> <div> <h1>{this.state.num2}</h1> <button onClick={() => this.handleAdd2(1)}> 1 </button> <button onClick={() => this.handleAdd2(2)}> 2 </button> <button onClick={() => this.handleAdd2(3)}> 3 </button> <button onClick={() => this.handleAdd2(4)}> 4 </button> <button onClick={() => this.handleAdd2(5)}> 5 </button> <button onClick={() => this.handleAdd2(6)}> 6 </button> <button onClick={() => this.handleAdd2(7)}> 7 </button> <button onClick={() => this.handleAdd2(8)}> 8 </button> <button onClick={() => this.handleAdd2(9)}> 9 </button> <button onClick={() => this.handleAdd2(0)}> 0 </button> </div> <div> <button onClick={() => this.handlePlus("+")}>+</button> <button onClick={() => this.handlePlus("-")}>-</button> <button onClick={() => this.handlePlus("x")}>x</button> </div> <div> <button onClick={this.handleClear}>Clear</button> <h1>{this.state.answer}</h1> <input type="text"></input> <button onClick={() => this.handleInput}>Enter !</button> <button onClick={() => console.log(this.state.equation)}> Log in console </button> </div> </div> ); } } ReactDOM.render(<App />, document.body);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>

If you check out the program, you will see that there are a few numbers, and if you click them and add, you will get the result in an h1.如果你查看程序,你会看到有几个数字,如果你点击它们并添加,你会得到一个 h1 的结果。 however, this is not that great because you can only add single digits.但是,这并不是很好,因为您只能添加个位数。 So, as you can see, I have added an input, which will take the equation and log it in the console(for now).因此,如您所见,我添加了一个输入,它将获取等式并将其记录在控制台中(暂时)。 But sadly, this is returning undefined.但遗憾的是,这是返回未定义。 Is there anything I can do to fix this issue?我能做些什么来解决这个问题吗?

You need to make the input a controlled component.您需要使输入成为受控组件。 You can make input controlled component by listening to the onChange event and passing value to the value attribute您可以通过监听onChange事件并将值传递给 value 属性来制作输入控制组件

<input type="text" value={this.state.equation} onChange={(e)=>this.setState({equation: e.target.value})}

Now you will be get the equation现在你将得到等式

you forgot to map the value of input tag, you can bind that with state, if you are new to react you should start using hooks thats more clear approach now,您忘记映射输入标签的值,您可以将其与状态绑定,如果您不熟悉反应,您应该开始使用钩子,这是现在更清晰的方法,
here is full working solution,这是完整的工作解决方案,
https://codesandbox.io/embed/inspiring-tesla-9sjop?fontsize=14&hidenavigation=1&theme=dark https://codesandbox.io/embed/inspiring-tesla-9sjop?fontsize=14&hidenavigation=1&theme=dark

import React, { Component } from "react";

class App extends React.Component {
  state = {
    num1: 0,
    num2: 0,
    answer: 0,
    equation: "",
    value: ""
  };

  handleChange = (e) => {
    this.setState({ equation: e.target.value });
  };

  handleAdd1 = (number) => {
    this.setState({ num1: number });
  };

  handleAdd2 = (number2) => {
    this.setState({ num2: number2 });
  };

  handleClear = () => {
    this.setState({ num1: 0, num2: 0, answer: 0 });
  };

  handlePlus = (sign) => {
    switch (sign) {
      case "+":
        this.setState({ answer: this.state.num1 + this.state.num2 });
        break;

      case "-":
        this.setState({ answer: this.state.num1 - this.state.num2 });
        break;

      case "x":
        this.setState({ answer: this.state.num1 * this.state.num2 });
        break;
    }
  };

  handleInput = (event) => {
    this.setState({ equation: event.target.value });
  };

  render() {
    return (
      <div>
        <div>
          <h1>{this.state.num1}</h1>
          <button onClick={() => this.handleAdd1(1)}> 1 </button>
          <button onClick={() => this.handleAdd1(2)}> 2 </button>
          <button onClick={() => this.handleAdd1(3)}> 3 </button>
          <button onClick={() => this.handleAdd1(4)}> 4 </button>
          <button onClick={() => this.handleAdd1(5)}> 5 </button>
          <button onClick={() => this.handleAdd1(6)}> 6 </button>
          <button onClick={() => this.handleAdd1(7)}> 7 </button>
          <button onClick={() => this.handleAdd1(8)}> 8 </button>
          <button onClick={() => this.handleAdd1(9)}> 9 </button>
          <button onClick={() => this.handleAdd1(0)}> 0 </button>
        </div>

        <div>
          <h1>{this.state.num2}</h1>
          <button onClick={() => this.handleAdd2(1)}> 1 </button>
          <button onClick={() => this.handleAdd2(2)}> 2 </button>
          <button onClick={() => this.handleAdd2(3)}> 3 </button>
          <button onClick={() => this.handleAdd2(4)}> 4 </button>
          <button onClick={() => this.handleAdd2(5)}> 5 </button>
          <button onClick={() => this.handleAdd2(6)}> 6 </button>
          <button onClick={() => this.handleAdd2(7)}> 7 </button>
          <button onClick={() => this.handleAdd2(8)}> 8 </button>
          <button onClick={() => this.handleAdd2(9)}> 9 </button>
          <button onClick={() => this.handleAdd2(0)}> 0 </button>
        </div>

        <div>
          <button onClick={() => this.handlePlus("+")}>+</button>
          <button onClick={() => this.handlePlus("-")}>-</button>
          <button onClick={() => this.handlePlus("x")}>x</button>
        </div>

        <div>
          <button onClick={this.handleClear}>Clear</button>
          <h1>{this.state.answer}</h1>
          <input
            type="text"
            value={this.state.equation}
            onChange={this.handleChange}
          ></input>
          <button onClick={() => this.handleInput}>Enter !</button>
          <button onClick={() => console.log(this.state.equation)}>
            Log in console
          </button>
        </div>
      </div>
    );
  }
}

export default App;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM