React POST data

by Allie Yu

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>

React

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

    this.state = {
      data: []
    };
  }

  componentDidMount() {
  	const url = 'https://jsonplaceholder.typicode.com/posts';
// The data we are going to send in our request

    let data = {
    	name: 'Sara'
}
// The parameters we are gonna pass to the fetch function
		let fetchData = { 
   	 method: 'POST', 
   	 body: data,
   	 headers: new Headers()
		}
	fetch(url, fetchData)
  .then(response => response.json())
	.then(data =>{console.log(data);  
  							this.setState({ data })
               })
    // Handle response you get from the server
}
  render(){
        return (
            <div>
                {this.state.data.map(dt => {
                return <div>{dt.title}</div>
                })}
            </div>
        )
    }
}

ReactDOM.render(<App/>, document.getElementById('root'))