fix(phone): Fix subsequent rerenders & added WDYR (#209)

Fixed an issue with infinite rerendering on app routes by making it a Pure component.

Adds `why-did-you-render` for development use.

Co-authored-by: Taso <[email protected]>
This commit is contained in:
J Francisco RaderandTaso authored and GitHub committed 2021-04-13 21:33:18 -04:00
1 parent 4fceb3750b
commit 8dc59c47bd
7 files changed
+51 -35

No files matched your search

@@ -1,10 +1,14 @@
import { useEffect } from 'react';
import { memo, useEffect } from 'react';
import { useNuiRequest } from 'fivem-nui-react-lib';
export const AppWithStartup = ({ children, id }) => {
const Component = ({ children, id }) => {
const Nui = useNuiRequest();
useEffect(() => {
Nui.send(`npwd:app:${id}`);
}, [Nui, id]);
return children;
};
const AppWithStartup = memo(Component, () => true);
export { AppWithStartup };
+1 -1
View File
@@ -67,7 +67,7 @@ export const APPS: IAppConfig[] = [
backgroundColor: MESSAGES_APP_PRIMARY_COLOR,
color: MESSAGES_APP_TEXT_COLOR,
path: '/messages',
Route: () => <AppRoute id="MESSAGE" path="/messages" component={MessagesApp} />,
Route: () => <AppRoute id="MESSAGES" path="/messages" component={MessagesApp} />,
},
{
id: 'CONTACTS',
@@ -2,11 +2,6 @@ import React from 'react';
import { Box, makeStyles } from '@material-ui/core';
import { CallControls } from './CallControls';
// interface ICallNotificationProps {
// onAccept(): void;
// onReject(): void;
// }
const useStyles = makeStyles((theme) => ({
root: {
paddingBottom: '48px',
@@ -49,15 +49,14 @@ const isKeyValid = (key) => validKeys.indexOf(key) !== -1;
export const useKeyboardService = () => {
const history = useHistory();
const { closePhone } = usePhone();
const getters = {
ArrowRight: useRecoilValue(keyboardState.ArrowRight),
ArrowLeft: useRecoilValue(keyboardState.ArrowLeft),
ArrowUp: useRecoilValue(keyboardState.ArrowUp),
ArrowDown: useRecoilValue(keyboardState.ArrowDown),
Backspace: useRecoilValue(keyboardState.Backspace),
Enter: useRecoilValue(keyboardState.Enter),
Escape: useRecoilValue(keyboardState.Escape),
};
const ArrowRight = useRecoilValue(keyboardState.ArrowRight);
const ArrowLeft = useRecoilValue(keyboardState.ArrowLeft);
const ArrowUp = useRecoilValue(keyboardState.ArrowUp);
const ArrowDown = useRecoilValue(keyboardState.ArrowDown);
const Backspace = useRecoilValue(keyboardState.Backspace);
const Enter = useRecoilValue(keyboardState.Enter);
const Escape = useRecoilValue(keyboardState.Escape);
const handlers = useRef(new Map());
const setEscape = useSetRecoilState<any>(keyboardState.Escape);
@@ -65,28 +64,29 @@ export const useKeyboardService = () => {
useEffect(
function registerCustomKeys() {
Object.keys(getters).forEach((g) => {
handlers.current.set(g, getters[g]);
});
handlers.current.set('ArrowRight', ArrowRight);
handlers.current.set('ArrowLeft', ArrowLeft);
handlers.current.set('ArrowUp', ArrowUp);
handlers.current.set('ArrowDown', ArrowDown);
handlers.current.set('Backspace', Backspace);
handlers.current.set('Enter', Enter);
handlers.current.set('Escape', Escape);
},
[getters],
[ArrowDown, ArrowLeft, ArrowRight, ArrowUp, Backspace, Enter, Escape],
);
useEffect(
function handleNUIKeyboardMessage() {
function onKeyUp(event) {
const { key } = event;
const callback = handlers.current.get(key);
if (isKeyValid(key) && callback && callback.call) {
return callback(event);
}
useEffect(function handleNUIKeyboardMessage() {
function onKeyUp(event) {
const { key } = event;
const callback = handlers.current.get(key);
if (isKeyValid(key) && callback && callback.call) {
return callback(event);
}
}
window.addEventListener('keyup', onKeyUp);
return () => window.removeEventListener('keyup', onKeyUp);
},
[getters],
);
window.addEventListener('keyup', onKeyUp);
return () => window.removeEventListener('keyup', onKeyUp);
}, []);
const backspaceHandler = useCallback(
(event) => {
+9
View File
@@ -0,0 +1,9 @@
/// <reference types="@welldone-software/why-did-you-render" />
import React from 'react';
if (process.env.NODE_ENV === 'development') {
const whyDidYouRender = require('@welldone-software/why-did-you-render');
whyDidYouRender(React, {
trackAllPureComponents: true,
});
}