Created
March 21, 2020 09:38
-
-
Save mdanshin/20d61d196f87caec05ba53054abe8f79 to your computer and use it in GitHub Desktop.
POST data to back-end
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {useState} from "react"; | |
export default function Create() { | |
const [state, setState] = useState() | |
function changeHandler(event) { | |
const {name, value} = event.target | |
setState(prevState => ({ | |
...prevState, | |
[name]: value | |
})) | |
} | |
function submitHandler(event) { | |
event.preventDefault() | |
fetch('api/create-server', { | |
method: 'POST', | |
body: JSON.stringify(state), | |
headers: { | |
'Content-Type': 'application/json;charset=utf-8' | |
}, | |
}) | |
} | |
return ( | |
<form onSubmit={submitHandler} autoComplete={"off"} method="POST"> | |
<input type="text" name="name" onChange={changeHandler}/> | |
<input type="text" name="IP" onChange={changeHandler}/> | |
<input type="text" name="OS" onChange={changeHandler}/> | |
<button>Post</button> | |
</form> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment