React
by Abdul Ahmad
HTML
<div id="app"></div>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
li {
margin: 8px 0;
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
.done {
color: rgba(0, 0, 0, 0.3);
text-decoration: line-through;
}
input {
margin-right: 5px;
}
React
function TodoApp() {
const { data, error, loading } = useCustomHook();
return (
<div>
</div>
);
}
function useCustomHook() {
const [data, setData] = React.useState('');
const [error, setError] = React.useState('');
const [loading, setLoading] = React.useState(false);
console.log('data', data);
console.log('error', error);
console.log('loading', loading);
React.useEffect(() => {
async function getData() {
try {
setLoading(true);
let data = await getAsyncData();
if (!data || data.length < 1) data = undefined;
setLoading(false);
setData(data);
} catch (e) {
setLoading(false);
setError(e);
}
}
getData();
});
return { data, loading, error };
}
function getAsyncData() {
return Promise.reject('async data');
}
ReactDOM.render(<TodoApp />, document.querySelector("#app"))