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, loading, error } = useCustomHook();
return (
<div></div>
);
}
const initialState = {
loading: false,
error: '',
data: undefined,
}
function useCustomHook() {
const [state, setState] = React.useState(initialState);
const { data, error, loading } = state;
console.log('data', data);
console.log('error', error);
console.log('loading', loading);
React.useEffect(() => {
async function getData() {
try {
setState({ ...state, loading: true });
let data = await getAsyncData();
if (!data || data.length < 1) data = undefined;
setState({ ...state, loading: false, data });
} catch (e) {
setState({ ...state, loading: false, error: e });
}
}
getData();
}, []);
return { data, loading, error };
}
function getAsyncData() {
return Promise.reject('async data');
}
function useComplexState(initialState) {
const [state, setState] = React.useState(initialState);
function
}
ReactDOM.render(<TodoApp />, document.querySelector("#app"));