I am trying to display an updated list of items every time a new item is added using the form. Using the useEffect hook I keep getting stuck in an infinite loop that crashes the page. I am not sure how to add some sort of validation that ask to re-render my component only if a new item has been added.
@app.route('/assets')
def get_assets():
print('this is a test')
cursor = assets.find()
list_cur = list(cursor)
assets = dumps(list_cur)
return assets
function Assets() {
const [currentAsset, setCurrentAsset] = useState(0);
useEffect(() => {
(async () => {
const result = await fetch('/assets')
const data = await result.json()
setCurrentAsset(data)
})()
}, [currentAsset]);
return (
<div>
<header>
<table className="table table-striped">
<thead>
<tr>
<th>Ip Address</th>
<th>Asset Type</th>
<th>Username</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
{Object.values(currentAsset).map((item, index) => (
<tr key={item._id? item._id.$oid: null}>
<td>{item.ip_address}</td>
<td>{item.asset_type}</td>
<td>{item.username}</td>
<td>{item.notes}</td>
</tr>
)
)}
</tbody>
</table>
</header>
</div>
);
}
export default Assets;
NOTE: I would like to renders updating data without reloading the page every time a new item has been added. I am trying to achieve the same result as this demo: https://taniarascia.github.io/react-hooks/ Is there a Hooks only approach to this?