简体   繁体   English

运行我的反应应用程序时收到此警告

[英]Getting this warning while running my react app

Warning: Failed prop type: You provided a value prop to a form field without an onChange handler.警告:失败的道具类型:您为没有onChange处理程序的表单字段提供了value道具。 This will render a read-only field.这将呈现一个只读字段。 If the field should be mutable use defaultValue .如果该字段应该是可变的,请使用defaultValue Otherwise, set either onChange or readOnly .否则,设置onChangereadOnly

AddContact.js AddContact.js

import React, { Component } from "react";

export default class AddContact extends Component {
  state = {
    name: "",
    email: "",
    phone: "",
  };

  onSubmit = (e) => {
    e.preventDefault();
    console.log(this.state);
  };
  onChanger = (e) => this.setState({ [e.target.name]: e.target.value });

  render() {
    // const { name, email, phone } = this.state;
    return (
      <div className="card mb-3">
        <div className="card-header">Add Contact</div>
        <div className="card-body">
          <form onSubmit={this.onSubmit}>
            <div className="form-group">
              <label htmlFor="name">Name</label>
              <input
                type="text"
                name="name"
                onChange={this.OnChanger}
                value={this.state.name}
                className="form-control form-control-lg"
                placeholder="Name.."
              />
            </div>
            <div className="form-group">
              <label htmlFor="email">Email</label>
              <input
                type="email"
                name="email"
                onChange={this.OnChanger}
                value={this.state.email}
                className="form-control form-control-lg"
                placeholder="Email.."
              />
            </div>
            <div className="form-group">
              <label htmlFor="phone">Phone</label>
              <input
                type="text"
                name="phone"
                onChange={this.OnChanger}
                value={this.state.phone}
                className="form-control form-control-lg"
                placeholder="Phone.."
              />
            </div>
            <input
              type="submit"
              value="Add Contact"
              className="btn btn-light btn-block"
            />
          </form>
        </div>
      </div>
    );
  }
}

Your last element should be a button not an input.您的最后一个元素应该是按钮而不是输入。

<input
  type="submit"
  value="Add Contact"
  className="btn btn-light btn-block"  />

Change it to <button type="submit" value="Add Contact" className="btn btn-light btn-block" />将其更改为<button type="submit" value="Add Contact" className="btn btn-light btn-block" />

暂无
暂无

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

相关问题 运行 eslint 时在“eslint-plugin-react-settings”中收到警告 - Getting warning in 'eslint-plugin-react-settings' while running eslint 运行 create-react-app 命令时出错 - Getting error while running create-react-app command 在我的反应 js 应用程序中将 css 添加到按钮时出错 - getting error while adding css to button in my react js app 如何在我的组件中添加一个键,同时在 React 中获取关键道具警告 - How do I add a key to my components, while getting the key prop warning in React 尝试运行我的应用程序时收到以下警告:Possible Unhandled Promise Rejection (id:1) - getting the following warning while trying to run my app: Possible Unhandled Promise Rejection (id:1) 在我的反应应用程序中引入了分页。 工作正常,但会出现一些减速和无响应的应用内导航。 未安装组件警告 - Introduced pagination into my react app. Works fine but getting some slowdown and unresponsive in-app navigation. Unmounted component warning 运行反应应用程序时出现类型错误,TypeError: instance.render is not a function - Getting type error while running react app, TypeError: instance.render is not a function 在React应用中运行&#39;npm start&#39;时出现不必要的转义字符警告 - Unnecessary escape character warning when running 'npm start' with React app 运行 create-react-app 时没有响应 - No response while running create-react-app 运行本机应用程序时出错 - Getting an error when running react native app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM