Merge pull request #41 from gaearon/smoother-right-pane

Avoid flashing "Loading..." in right pane
This commit is contained in:
Brian Vaughn
2019-04-02 10:09:43 -07:00
committed by GitHub

View File

@@ -30,7 +30,9 @@ export default function SelectedElement(_: Props) {
const element =
selectedElementID !== null ? store.getElementByID(selectedElementID) : null;
const inspectedElement = useInspectedElement(selectedElementID);
const actualInspectedElement = useInspectedElement(selectedElementID);
// The UI lags behind to avoid flashing a loading state.
const inspectedElement = useLastNonNullValue(actualInspectedElement);
const highlightElement = useCallback(() => {
if (element !== null && selectedElementID !== null) {
@@ -284,3 +286,11 @@ function useInspectedElement(id: number | null): InspectedElement | null {
return inspectedElement;
}
function useLastNonNullValue<T>(value: T | null): T | null {
const [lastNonNullValue, setLastNonNullValue] = useState(value);
if (value !== null && value !== lastNonNullValue) {
setLastNonNullValue(value);
}
return lastNonNullValue;
}