K
K
kamelot432020-09-03 14:15:20
JavaScript
kamelot43, 2020-09-03 14:15:20

How to send a form to the specified address in react?

In react, when working with a form, if we use third-party libraries (reactbootstrap or antdesign for example), how is the form submitted? This refers to the address to which the form should go when filling in the fields. This issue is not very detailed in the documentation, there are questions about how it works in real life. For example, in a normal html form, the action and method attributes indicate the address and the submission method. How does it work in react?
On the documentation pages
https://react-bootstrap.github.io/components/forms... and https://ant.design/components/form/#API didn't find anything suitable.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Bektur Muratov, 2020-09-03
@prosto_paren

As an option, submitting the form can be implemented by hanging an event listener on the submit button

import React, { useRef } from 'react'

const form=()=> {

    // создаем пустую ссылку для того чтобы потом привязать к форме
    const formRef = useRef() 

    const onBtnClick=()=>{
       let data= formRef.current  // при нажатии кнопки получаем обрабатываем данные формы 
       axios.post('/user', {
          data
       } )
        .then(function (response) {
          console.log(response);
        })
        .catch(function (error) {
          console.log(error);
        }); 
    }

    return (
        <form id='form' ref={formRef}>    //  привязывает ссылку к элементу   
            <label >
                <input type="text" name="firstName" id="firstName"/>
                Имя
            </label>
            <label >
                <input type="text" name="lastName" id="lastName"/>
                Фамилия
            </label>
            <button onClick={onBtnClick  }>Отправить</button>/>
        </form>
    )
}

export default form

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question