B
B
brainexplosion2018-03-01 14:57:42
React
brainexplosion, 2018-03-01 14:57:42

How to make a request to the server in React?

There is a form where data is entered (first name, last name, etc.). How to send this data to the server in React?

class App extends React.Component {
  constructor() {
    super();
    this.state = {value: ''};
  }

  handleChange(event) {
    this.setState({value: event.target.value});
  }

  handleSubmit(event) {
    alert(this.state.value); 
    event.preventDefault(); 
  }

  render() {
    return <form onSubmit={this.handleSubmit.bind(this)}>
                        <label>
                            Name:
      <input value={this.state.value} onChange={this.handleChange.bind(this)} />
                        </label>
      <input type="submit" />
    </form>
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Aleksandrovich, 2018-03-01
@brainexplosion

you can put a post with your data on the server into the handleSubmit function using fetch or axios

A
Archakov Dennis, 2018-03-01
@archakov06

How to use ReactJS with PHP?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question