use callback

This commit is contained in:
Yostra 2020-09-09 16:14:59 -07:00 committed by Gene Hoffman
parent a5a9ed3695
commit 6688d653e5
2 changed files with 34 additions and 33 deletions

View File

@ -54,28 +54,28 @@ const Block = props => {
const [headerHash, setHeaderHash] = useState("");
const [plotId, setPlotId] = useState("");
const [didMount, setDidMount] = useState(false);
const prev_header_hash = props.block.header.data.prev_header_hash;
const height = props.block.header.data.height;
const dispatch = useDispatch();
const handleClearBlock = useCallback(
() => dispatch(clearBlock()),
() => dispatch(clearBlock()),
[dispatch]
);
const handleGetHeader = useCallback(
(headerHash) => dispatch(getHeader(headerHash)),
(headerHash) => dispatch(getHeader(headerHash)),
[dispatch]
);
const handleGetBlock = useCallback(
(headerHash) => dispatch(getBlock(headerHash)),
(headerHash) => dispatch(getBlock(headerHash)),
[dispatch]
);
);
const fetchHeaderIfNecessary = async () => {
const fetchHeaderIfNecessary = useCallback(async () => {
if (props.prevHeader) {
const phh = await hash_header(props.prevHeader);
if (phh !== props.block.header.data.prev_header_hash) {
@ -94,13 +94,14 @@ const Block = props => {
var newPlotId = arr_to_hex(bufHash);
setHeaderHash(newHeaderHash);
setPlotId(newPlotId);
}
}, [handleGetHeader, props])
useEffect((prevProps) => {
useEffect((prevProps) => {
(async () => {
if (!didMount || height > 0) {
await fetchHeaderIfNecessary();
}}) ()
}
})()
}, [prev_header_hash, height, didMount, setDidMount, fetchHeaderIfNecessary]);
const classes = props.classes;
@ -223,14 +224,14 @@ const Block = props => {
></HelpIcon>
</Tooltip>
) : (
""
)}
""
)}
</TableCell>
<TableCell
onClick={
row.name === "Previous block"
? () => handleGetBlock(row.value)
: () => {}
: () => { }
}
align="right"
>

View File

@ -1,4 +1,4 @@
import React, { useEffect, useState } from "react";
import React, { useEffect, useState, useCallback } from "react";
import Grid from "@material-ui/core/Grid";
import { makeStyles, withStyles } from "@material-ui/core/styles";
import Container from "@material-ui/core/Container";
@ -191,8 +191,8 @@ const StatusCell = props => {
<HelpIcon style={{ color: "#c8c8c8", fontSize: 12 }}></HelpIcon>
</Tooltip>
) : (
""
)}
""
)}
</Box>
</Box>
</div>
@ -282,8 +282,8 @@ const Challenges = props => {
<TableCell align="right">
{item.estimates.length > 0
? Math.floor(
Math.min.apply(Math, item.estimates) / 60
).toString() + " minutes"
Math.min.apply(Math, item.estimates) / 60
).toString() + " minutes"
: ""}
</TableCell>
</TableRow>
@ -488,8 +488,8 @@ const Plots = props => {
</List>{" "}
</span>
) : (
""
)}
""
)}
{failed_to_open_filenames.length > 0 ? (
<span>
<div className={classes.cardTitle}>
@ -521,8 +521,8 @@ const Plots = props => {
</List>
</span>
) : (
""
)}
""
)}
</Grid>
</Grid>
<Dialog
@ -613,7 +613,7 @@ const Farmer = props => {
const classes = props.classes;
const checkRewards = async () => {
const checkRewards = useCallback(async () => {
let totalChia = BigInt(0);
let biggestHeight = 0;
for (let wallet of wallets) {
@ -654,16 +654,16 @@ const Farmer = props => {
setTotalChiaFarmed(totalChia);
setBiggestHeight(biggestHeight);
}
};
}, [totalChiaFarmed, didMount, wallets]);
useEffect(() => {
(async () => {
await checkRewards();
if (!didMount) {
setDidMount(true);
useEffect(() => {
(async () => {
await checkRewards();
if (!didMount) {
setDidMount(true);
dispatch(clearSend);
}
}) ()
})()
}, [checkRewards, dispatch, props, didMount, setDidMount]);
return (
@ -676,7 +676,7 @@ const Farmer = props => {
></FarmerContent>
</Container>
</main>
</div>
</div>
);
}