Send an Email Using React.js + Express.js

前端 未结 2 846
谎友^
谎友^ 2020-12-23 23:33

I have built a web app using React.js in ES6. I currently want to create a basic \"Contact Us\" page and want to send an email. I am new to React and just discovered that I

2条回答
  •  一整个雨季
    2020-12-24 00:19

    @ryan-jenkin is completely correct.

    Alternatively, if you don't have / want jQuery as a dependency, you can use the native fetch api. Also, I typically set up my form so each input has a state, then use that state in the stringified blob.

    Client-side (React):

    handleSubmit(e){
      e.preventDefault()
    
      fetch('/contactus', {
        method: 'POST',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          email: this.state.email,
          // then continue this with the other inputs, such as email body, etc.
        })
      })
      .then((response) => response.json())
      .then((responseJson) => {
        if (responseJson.success) {
          this.setState({formSent: true})
        }
        else this.setState({formSent: false})
      })
      .catch((error) => {
        console.error(error);
      });
    }
    
    render(){
      return (
        
    // continue this with other inputs, like name etc
    ) }

提交回复
热议问题