Guest User

Untitled

a guest
Jul 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. class DogForm extends Component {
  2. constructor(props) {
  3. super(props)
  4. this.state = {
  5. name: "",
  6. toy: "",
  7. nickname: ""
  8.  
  9. }
  10. this.formRef = null;
  11. }
  12.  
  13. handlePostRequest = (event) => {
  14. const state = this.state
  15. fetch(URL,{
  16. method:"POST",
  17. body: JSON.stringify({
  18. name:`${state.name}`,
  19. toy:`${state.toy}`,
  20. nickname:`${state.nickname}`
  21. }),
  22. headers: new Headers({
  23. "content-type" : "application/json"
  24. })
  25. }).then(this.reset)
  26. .catch(err => {
  27. console.error(err)
  28. })
  29.  
  30. }
  31. reset = () => {
  32. this.setState({
  33.  
  34. name:"",
  35. toy: "",
  36. nickname: ""
  37. })
  38. }
  39.  
  40. render() {
  41. return (
  42. <Container style={style.container}>
  43. <Content >
  44. <Form className="profile-form">
  45. <Item inlineLabel >
  46. <Label>Dog Name</Label>
  47. <Input onChangeText={(name) => this.setState({name})} defaultValue={""} value={this.state.name} />
  48. </Item>
  49. <Item inlineLabel>
  50. <Label>Favorite Toy</Label>
  51. <Input onChangeText={(toy) => this.setState({toy})} defaultValue={""} value={this.state.toy} />
  52. </Item>
  53. <Item inlineLabel last>
  54. <Label>Nickname</Label>
  55. <Input onChangeText={(nickname) => this.setState({nickname})} defaultValue={""} value={this.state.nickname} />
  56. </Item>
  57. <Button block primary onPress={(event) => {this.handlePostRequest(event);}} >
  58. <Text>Create Dog Profile</Text>
  59. </Button>
  60. </Form>
  61. </Content>
  62. </Container>
  63. )
  64. }
  65. }
Add Comment
Please, Sign In to add comment