4
import React from 'react';
import {Jumbotron} from 'react-bootstrap';
import axios from 'axios';
import './App.css'

export default class Form extends React.Component{

state={
    user:[],
    firstname:'',
    lastname:'',
}
handleChange = event => {
    this.setState({firstname:event.target.value});
    this.setState({lastname:event.target.value});
}

handleSubmit = event =>{
    event.preventDefault();

    axios.post('http://9795ca45.ngrok.io/api/Docs',{
        fname:this.state.firstname,
        lname:this.state.lastname
    })
    .then( res => {
        console.log(res);
        console.log(res.data);
    })
    .catch((error) =>{
        alert(error);
    })
}

componentDidMount(){
    axios.get('http://9795ca45.ngrok.io/api/Docs')
    .then(res =>{
        const data=res.data;

        const user = data.data;

        this.setState({user});
    })
    .catch((error) =>{
        alert(error);
    })
}

render(){
    return(
        <div>
            <Jumbotron>
                <form onSubmit={this.handleSubmit}>
                <label>
                    firstName:
                    <input type="text" name="firstname" onChange={this.handleChange} />
                    </label>
                <label>
                    LastName:
                    <input type="text" name="lastname" onChange={this.handleChange} />
                    </label>
                    <button type="submit">add</button>
                    </form>
            
                <h1> names </h1>
                <ul> {this.state.user.map(person => <li> {person.lname}</li>)}</ul>
                </Jumbotron>
                </div>
    )
}
}

am very new to reatjs am trying to post the data to the url but am getting request not found 500 error and i see this using debugger

Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of Form.

please help me to come away from this

am getting the data from the url but i unable to post the data

please check it and correct my code with an explanation

Sarun UK
  • 6,210
  • 7
  • 23
  • 48
  • am getting an error after clicking the button.. while trying to post.. –  Mar 22 '18 at 10:03
  • previous question is to ask about get the data.. this time am facing the problem while posting the data. –  Mar 22 '18 at 10:03
  • Which previous question? – Chris Mar 22 '18 at 10:05
  • previous question is solved .. whenever am trying to post only am geting request status failed 500 error –  Mar 22 '18 at 10:06
  • You are answering "previous question" when I am asking you what you mean by "previous question"... Do you mean your post has been answered? In that case post a new question please. – Chris Mar 22 '18 at 10:09
  • yes previous question is answered.. this is a new question but in the same program.. the question i was unable to post the data, am getting request not found 500 error –  Mar 22 '18 at 10:11
  • Alright, but your title is about unique key prop. What you have here is not relevant to that error. So please either modify your original question or post a new one. – Chris Mar 22 '18 at 10:14