Skip to content

Instantly share code, notes, and snippets.

@stoneboyindc
Created August 19, 2020 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stoneboyindc/5943b3c1297a6de07c7f24c15054ede1 to your computer and use it in GitHub Desktop.
Save stoneboyindc/5943b3c1297a6de07c7f24c15054ede1 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import dataContext from './dataContext';
export default class AddFolder extends Component{
static contextType=dataContext;
handleSubmit=e=>{
e.preventDefault()
const name=e.target.folder;
console.log(name.value,name);
let newFolder = { "name" : name.value};
fetch(`http://localhost:9090/folders`,{
method:'POST',
body:JSON.stringify(newFolder),
headers:{'Content-Type':'application/json'}
}).then(res=>{
if(!res.ok){return res.json().then(err=>{throw err})}
return res.json()
}).then(resJson=>{
console.log(resJson)
this.context.addFolder(resJson)
}).catch(error=>{console.log(error)})
}
render(){
const data = this.context;
return(
<div className="addFolder">
<form onSubmit={this.handleSubmit}>
<label htmlFor="folder" >Folder Name</label>
<input type='text' name="folder"
id="folder" required/>
<button type='submit'>
Submit
</button>
</form>
</div>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment