简体   繁体   English

如何合并两个 onChange 事件?

[英]How can I merge two onChange events?

I have an API that receives data from a form For the telephone input, it requires a country code.我有一个 API 从表单接收数据对于电话输入,它需要国家代码。 So then I set up the react-phone-number-input to format the phone numbers sent through the input but it ended up conflicting with the API post request because both the react-phone-number-input & the post request uses an onChange event handler to work因此,我设置了 react-phone-number-input 以格式化通过输入发送的电话号码,但它最终与 API 发布请求冲突,因为 react-phone-number-input 和发布请求都使用 onChange 事件处理程序工作

So now, the react-phone-number-input works.所以现在,react-phone-number-input 起作用了。 But I can't send the values to the API但我无法将值发送到 API

How can I fix this, please also, ignore if the API works or not.我该如何解决这个问题,请忽略 API 是否有效。 what I need is for it to console log the values first then I'll sort out the API issue myself我需要的是它先控制台记录值,然后我自己解决 API 问题

Thanks in advance提前致谢

codesanbox link to code below: https://codesandbox.io/s/verigo-forked-lh4i7?file=/src/pages/DeliveryExecutive.js codesanbox 链接到以下代码: https://codesandbox.io/s/verigo-forked-lh4i7?file=/src/pages/DeliveryExecutive.js

function DeliveryExecutive() {
const [phoneValue, setPhoneValue] = useState();
const [formData, setFormData] = useState({
    userType: 4,
    name: "",
    surname: "",
    phoneNumber: "",
    email: "",
    password: "",
    referralCode: "" });

const {
    name,
    surname,
    phoneNumber,
    email,
    password,
    referralCode
  } = formData;


const onInputChange = (e) => {
    const value =
      e.target.type === "checkbox" ? e.target.checked : e.target.value;

    setFormData({
      ...formData,
      [e.target.name]: value
    });
  };



const onSubmit = async (e) => {
    e.preventDefault();
    console.log("formData", { ...formData });

    const { data } = await axios.post(
      "http://api.myverigo.com/api/services/app/Account/User",
      formData,
      {
        headers: {
          clientid: "Client id",
          clientsecret: "client secret"
        }
      }
    );

    console.log("response data", data);
  };

return (
<div>
  <form id="login-form" onSubmit={onSubmit}>
  <div class="form-group">
      <input
                  type="tel"
                  name="phoneNumber"
                  value={phoneNumber}
                  onChange={onInputChange}
                  id="phone"
                  className="de-input form-control leave-message-input rounded-pill"
                  placeholder="Enter your mobile number"
                  required
                />
      
    </div>
    <div class="form-group">
      <PhoneInput
        className="de-input form-control leave-message-input rounded-pill"
        placeholder="Enter your mobile number"
        name="phoneNumber"
        country="NG"
        value={phoneValue}
        onChange={setPhoneValue}
        required
      />
    </div>

    <button type="submit" class="btn login-btn rounded-pill">
      Become a partner
    </button>
  </form>
</div>
  );
}
const firstFunc = async () => {
   await //do some stuff
   secondFunc()
}

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

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