我是新来的反应,当我点击提交按钮时没有任何反应?

I am new to react and when i click on submit button nothing happens?

这是代码!! 实际上对 React 很陌生,我正在做一个关于待办事项列表的项目,但是在执行此操作时,我一直在渲染列表项中输入字段的输出。如果有另一个解决方案,比如没有列表组,那将非常有帮助!

我在哪里犯了真正的错误,请有人看看这个 提前致谢!

import React, { Component, Fragment } from "react";
class MainPage extends Component {
  state = { data: "" };
  handleChange = (e) => {
    e.preventDefault();};
  handleSubmit = (e) => {
    e.preventDefault();

    this.setState({ data: e.target.value });
  };



render() {
    const mystyle = {
      padding: "16px 16px 16px 60px",
      textAlign: "start",
      fontSize: "24px",
      fontFamily: 'Helvetica Neue", Helvetica, Arial, sans-serif',
      width: "500px",
    };

return (
  <Fragment>
    <h1 className="display-1 text-center" style={{ color: "#f7c6c6" }}>
      todos
    </h1>
    <form className="todo-form" onSubmit={this.handleSubmit}>
      <input
        type="text"
        onChange={this.handleChange.bind(this)}
        className="new-todo shadow-lg p-3 mb-5 bg-white rounded"
        style={mystyle}
        placeholder="What needs to be done?"
      />

      <button className="btn btn-primary btn-sm ml-3">Submit</button>

      <ul className="list-group">
        <li
          className="list-group-item disabled p-3 mb-5 w-50 p-3 mx-auto "
          style={{ width: "200px", fontSize: "24px" }}
        >
          {this.state.data}
        </li>
      </ul>
    </form>
  </Fragment>
);}}

导出默认主页;

import React, { Component, Fragment } from "react";
class MainPage extends Component {
  state = { data: "" };

  handleSubmit = (e) => {
    e.preventDefault();

    alert(data)
  };



render() {
    const mystyle = {
      padding: "16px 16px 16px 60px",
      textAlign: "start",
      fontSize: "24px",
      fontFamily: 'Helvetica Neue", Helvetica, Arial, sans-serif',
      width: "500px",
    };

return (
  <Fragment>
    <h1 className="display-1 text-center" style={{ color: "#f7c6c6" }}>
      todos
    </h1>
    <form className="todo-form" onSubmit={this.handleSubmit}>
      <input
        type="text"
        onChange={(e) => this.setState({data: e.target.value})}
        className="new-todo shadow-lg p-3 mb-5 bg-white rounded"
        style={mystyle}
        placeholder="What needs to be done?"
      />

      <button className="btn btn-primary btn-sm ml-3">Submit</button>

      <ul className="list-group">
        <li
          className="list-group-item disabled p-3 mb-5 w-50 p-3 mx-auto "
          style={{ width: "200px", fontSize: "24px" }}
        >
          {this.state.data}
        </li>
      </ul>
    </form>
  </Fragment>
);}}

试一试:

state = { data: "", FinalDataValue:"" };
 handleChange = (e) => {
e.preventDefault();
this.setState({ data: e.target.value });};


handleSubmit = (e) => {
e.preventDefault();
this.setState({ FinalDataValue:this.state.data });};

并在渲染列表中这样写:

<li
      className="list-group-item disabled p-3 mb-5 w-50 p-3 mx-auto "
      style={{ width: "200px", fontSize: "24px" }}
    >
      {this.state.FinalDataValue}
    </li>

您的代码中存在一些问题:

  1. 您想在按钮上提交。为此,您需要添加 type="submit"
<button className="btn btn-primary btn-sm ml-3" type="submit">
    Submit
</button>

  1. 您需要维护两个状态,第一个用于 input,第二个用于 todo's。这样,当用户提交表单时,您总是可以追加现有的待办事项:
state = { data: [], input: "" };
  1. 最后你正在保存值 onSubmit 但它没有 e.target.value 而你需要保存在 onHandleChage

完整代码如下:

import React, { Component, Fragment } from "react";

class MainPage extends Component {
  state = { data: [], input: "" };
  handleChange = (e) => {
    e.preventDefault();
    this.setState({ input: e.target.value });
  };
  handleSubmit = (e) => {
    e.preventDefault();
    this.setState({ data: [...this.state.data, this.state.input], input: "" });
  };

  render() {
    const mystyle = {
      padding: "16px 16px 16px 60px",
      textAlign: "start",
      fontSize: "24px",
      fontFamily: 'Helvetica Neue", Helvetica, Arial, sans-serif',
      width: "500px"
    };

    return (
      <Fragment>
        <h1 className="display-1 text-center" style={{ color: "#f7c6c6" }}>
          todos
        </h1>
        <form className="todo-form" onSubmit={this.handleSubmit}>
          <input
            type="text"
            onChange={this.handleChange.bind(this)}
            value={this.state.input}
            className="new-todo shadow-lg p-3 mb-5 bg-white rounded"
            style={mystyle}
            placeholder="What needs to be done?"
          />

          <button className="btn btn-primary btn-sm ml-3" type="submit">
            Submit
          </button>

          <ul className="list-group">
            {this.state.data.map((data, i) => {
              return (
                <li
                  className="list-group-item disabled p-3 mb-5 w-50 p-3 mx-auto "
                  style={{ width: "200px", fontSize: "24px" }}
                  key={"todo-" + i}
                >
                  {data}
                </li>
              );
            })}
          </ul>
        </form>
      </Fragment>
    );
  }
}

export default function App() {
  return (
    <div className="App">
      <MainPage />
    </div>
  );
}

这是演示:https://codesandbox.io/s/xenodochial-banzai-qwxfu?file=/src/App.js:0-1762