Last active
July 11, 2019 15:58
-
-
Save jmas/24e0c36fbf907a8c1de66f3651c0ea31 to your computer and use it in GitHub Desktop.
react-application.js
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 App({ data }) { | |
const [ text, setText ] = useState(data.isChecked); | |
const [ error, setError ] = useState(null); | |
const [ isSuccess, setIsSuccess ] = useState(false); | |
const [ inProgress, setInProgress ] = useState(false); | |
function submitForm() { | |
setIsSuccess(false); | |
setInProgress(true); | |
window.fetch('/send', { | |
body: { text } | |
}).then(response => { | |
if (response.status !== 200) { | |
throw new Error(`Failed!`); | |
} | |
return response.json(); | |
}).then( | |
data => { | |
setInProgress(false); | |
setIsSuccess(true); | |
}, | |
error => { | |
setInProgress(false); | |
setError(error); | |
} | |
); | |
} | |
return ( | |
<form onSubmit={event => { | |
event.preventDefault(); | |
submitForm(); | |
}}> | |
<input type={'text'} onChange={event => setText(event.target.value)} /> | |
<button>Send</button> | |
</form> | |
); | |
} |
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 from 'react'; | |
import ReactDOM from 'react-dom'; | |
import App from '/app'' | |
ReactDOM.render( | |
document.getElementById('container'), | |
<App isChecked={document.getElementById('container').dataset} /> | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment