简体   繁体   English

如何在按钮上运行警报单击React.js

[英]How to run an alert on button click React.js

I've been running through a react file upload tutorial and want to add on to it. 我一直在运行一个反应文件上传教程,并希望添加它。 I'm trying to make it so when a user clicks the upload message the browser will prompt them saying "Your file is being uploaded" I'm not a frontend dev by any means so please forgive me if this question is super nooby. 我试图这样做,当用户点击上传消息时,浏览器会提示他们说“你的文件正在上传”我不是任何方式的前端开发者所以请原谅我,如果这个问题超级高兴。 For some reason when I use this code, if you navigate to the web page the code in the function runs once, and then again on click. 出于某种原因,当我使用此代码时,如果您导航到网页,该函数中的代码将运行一次,然后再次单击。 My intended use is to only run on click, any idea what I am doing wrong? 我的用途是只运行点击,任何想法我做错了什么?

import React, { Component } from 'react'
import { Alert } from 'react-alert'

class Main extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      imageURL: '',
    };

    this.handleUploadImage = this.handleUploadImage.bind(this);
  }

  handleUploadImage(ev) {
    ev.preventDefault();

    const data = new FormData();
    data.append('file', this.uploadInput.files[0]);
    data.append('filename', this.fileName.value);

    fetch('http://localhost:8000/upload', {
      method: 'POST',
      body: data,
    }).then((response) => {
      response.json().then((body) => {
        this.setState({ imageURL: `http://localhost:8000/${body.file}` });
      });
    });
  }

  render() {
    return (
      <form onSubmit={this.handleUploadImage}>
        <div>
          <input ref={(ref) => { this.uploadInput = ref; }} type="file" />
        </div>
        <div>
          <input ref={(ref) => { this.fileName = ref; }} type="text" placeholder="Enter the desired name of file" />
        </div>
        <br />
        <div>
          <button onclick="myFunction()">Upload</button>
              <script>
              function myFunction() {
                  alert("Your file is being uploaded!")
              }
              </script>

        </div>
        <img src={this.state.imageURL} alt="img" />
      </form>
    );
  }
}

export default Main;

Why don't you move the alert at the begining of handleUploadImage function? 为什么不在handleUploadImage函数开头移动警报?

handleUploadImage(ev) {
    alert("Your file is being uploaded!")
    ....
}

and instead of 而不是

<div>
  <button onclick="myFunction()">Upload</button>
      <script>
      function myFunction() {
          alert("Your file is being uploaded!")
      }
      </script>

</div>

you will have 你将会有

<div>
  <button type="submit">Upload</button>
</div>

for anyone curious, here is the final code following the help I received. 对于任何好奇的人,这是我收到的帮助后的最终代码。

import React, { Component } from 'react'
import { Alert } from 'react-alert'

class Main extends React.Component {


  constructor(props) {
    super(props);

    this.state = {
      imageURL: '',
    };

    this.handleUploadImage = this.handleUploadImage.bind(this);
  }

  handleUploadImage(ev) {
    alert("Your file is being uploaded!")
    ev.preventDefault();

    const data = new FormData();
    data.append('file', this.uploadInput.files[0]);
    data.append('filename', this.fileName.value);

    fetch('http://localhost:8000/upload', {
      method: 'POST',
      body: data,
    }).then((response) => {
      response.json().then((body) => {
        this.setState({ imageURL: `http://localhost:8000/${body.file}` });
      });
    });
  }

  render() {
    return (
      <form onSubmit={this.handleUploadImage}>
        <div>
          <input ref={(ref) => { this.uploadInput = ref; }} type="file" />
        </div>
        <div>
          <input ref={(ref) => { this.fileName = ref; }} type="text" placeholder="Enter the desired name of file" />
        </div>
        <br />
        <div>
          <button type="submit">Upload</button>
        </div>
        <img src={this.state.imageURL} alt="img" />
      </form>
    );
  }
}

export default Main;

Change 更改

<form onSubmit={this.handleUploadImage}>

To

<form onSubmit={e => this.handleUploadImage(e)}>

This will call handleUploadImage only when you click 这将仅在您单击时调用handleUploadImage

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

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