Skip to content

Instantly share code, notes, and snippets.

@Noah-Huppert
Last active January 29, 2021 04:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Noah-Huppert/82584d844385ed968b8d89ec41e05778 to your computer and use it in GitHub Desktop.
Save Noah-Huppert/82584d844385ed968b8d89ec41e05778 to your computer and use it in GitHub Desktop.
React computer properties based on state items.
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