Skip to content

Commit

Permalink
feat:implements-custom-hook-to-force-component-re-render
Browse files Browse the repository at this point in the history
  • Loading branch information
Enzo707 committed May 26, 2023
1 parent ab0250b commit b445ab2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import '@hig/fonts/build/ArtifaktElement.css';
import './App.css';
import React, { useEffect, useState } from 'react';
import { useReRender } from './hooks';
import NotificationsPanel from '@dynamods/notifications-panel';
import { EmptyStateArchiver } from './icons';
import Timestamp from '@hig/timestamp';
import axios from 'axios';

function App() {
const [APIData, setAPIData] = useState({ loaded: false, notifications: [], title: 'Notifications', bottomButtonText: 'Mark all as read' });
const [componentIsLoaded, setComponentIsLoaded] = useState(false);
const forceRender = useReRender();

useEffect(() => {
if (process.env.NOTIFICATION_URL) {
Expand All @@ -31,9 +32,8 @@ function App() {
}, []);

const setPopupHeight = () => {
if (chrome.webview !== undefined) {
chrome.webview.hostObjects.scriptObject.UpdateNotificationWindowSize(document.body.scrollHeight);
}
if(chrome.webview === undefined) return;
chrome.webview.hostObjects.scriptObject.UpdateNotificationWindowSize(document.body.scrollHeight);
};

useEffect(()=> {
Expand All @@ -53,7 +53,7 @@ function App() {
};

const notificationChanged = () => {
setComponentIsLoaded(prevState=> !prevState);
forceRender();
};

const parseNotifications = (notifications) => {
Expand Down
1 change: 1 addition & 0 deletions src/hooks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as useReRender } from './use-re-render';
8 changes: 8 additions & 0 deletions src/hooks/use-re-render.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { useReducer } from 'react';
// This custom hook allow us to force component re-render
const useReRender = () => {
const [, reRender] = useReducer((x) => x + 1, 0);
return reRender;
};

export default useReRender;
5 changes: 0 additions & 5 deletions tests/e2e/e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,4 @@ describe('App (e2e)', () => {
it('should have correct title', async () => {
await expect(page.title()).resolves.toMatch('Notifications App');
});

it('should have correct header', async () => {
const header = await page.waitForSelector('#root > div > div > header');
await expect(header).toMatch('Notifications');
});
});

0 comments on commit b445ab2

Please sign in to comment.