Last active
January 29, 2021 04:34
-
-
Save catvec/82584d844385ed968b8d89ec41e05778 to your computer and use it in GitHub Desktop.
React computer properties based on state items.
This file contains hidden or 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, { | |
useEffect, | |
useState, | |
} from "react"; | |
const App = () => { | |
const [errs, setErrs] = useState([]); | |
/** | |
* Helper which just appends the e to errs. | |
*/ | |
const showErr = (e) => { | |
const newErrs = [...errs, e]; | |
console.log("in showErr, errs=", errs, "newErrs=", newErrs); | |
setErrs(newErrs); | |
}; | |
useEffect(() => { | |
showErr("this is the first error"); | |
showErr("this is the second error"); | |
}, []); | |
return ( | |
<> | |
<div>header</div> | |
{errs.map((e, i) => { | |
return ( | |
<div key={i}> | |
Error: {e} | |
</div> | |
); | |
})} | |
<div>content</div> | |
<div>footer</div> | |
</> | |
); | |
}; | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment