Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid unnecessary rerenders React components #3138

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/dry-olives-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'graphiql': patch
'@graphiql/plugin-explorer': patch
'@graphiql/react': patch
---

avoid unecessary renders by using useMemo or useCallback
9 changes: 8 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ module.exports = {
},

rules: {
'@arthurgeron/react-usememo/require-usememo': [
'error',
{
checkHookCalls: false,
},
],
// Possible Errors (http://eslint.org/docs/rules/#possible-errors)
'no-console': 'error',
'no-constant-binary-expression': 2,
Expand Down Expand Up @@ -302,7 +308,7 @@ module.exports = {
'@typescript-eslint/no-namespace': 'off',
},

plugins: ['promise', 'sonarjs', 'unicorn'],
plugins: ['promise', 'sonarjs', 'unicorn', '@arthurgeron/react-usememo'],

overrides: [
{
Expand Down Expand Up @@ -337,6 +343,7 @@ module.exports = {
rules: {
'jest/no-conditional-expect': 'off',
'jest/expect-expect': ['error', { assertFunctionNames: ['expect*'] }],
'@arthurgeron/react-usememo/require-usememo': 'off',
},
},
{
Expand Down
2 changes: 2 additions & 0 deletions custom-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ LekoArts

// packages and tools
argparse
arthurgeron
atpl
browserslist
bundlephobia
Expand Down Expand Up @@ -104,6 +105,7 @@ templayed
typedoc
twing
undici
usememo
velocityjs
vite
vitejs
Expand Down
49 changes: 24 additions & 25 deletions examples/graphiql-parcel/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
import ReactDOM from 'react-dom';
import { render } from 'react-dom';
import { GraphiQL } from 'graphiql';
import type { Fetcher } from '@graphiql/toolkit';
import { CSSProperties } from 'react';

const App = () => (
<GraphiQL
style={{ height: '100vh' }}
fetcher={async graphQLParams => {
const data = await fetch(
'https://swapi-graphql.netlify.app/.netlify/functions/index',
{
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(graphQLParams),
credentials: 'same-origin',
},
);
return data.json().catch(() => data.text());
}}
/>
);
const fetcher: Fetcher = async graphQLParams => {
const data = await fetch(
'https://swapi-graphql.netlify.app/.netlify/functions/index',
{
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(graphQLParams),
credentials: 'same-origin',
},
);
return data.json().catch(() => data.text());
};

ReactDOM.render(<App />, document.getElementById('root'));
const style: CSSProperties = { height: '100vh' };

const App = () => <GraphiQL style={style} fetcher={fetcher} />;

render(<App />, document.getElementById('root'));

// Hot Module Replacement
if (module.hot) {
module.hot.accept();
}
module.hot?.accept();
45 changes: 27 additions & 18 deletions examples/graphiql-webpack/src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ ${getQuery(arg, 2)}

const snippets = [exampleSnippetOne, exampleSnippetTwo];

const fetcher = async (graphQLParams, options) => {
const data = await fetch(
'https://swapi-graphql.netlify.app/.netlify/functions/index',
{
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
...options.headers,
},
body: JSON.stringify(graphQLParams),
credentials: 'same-origin',
},
);
return data.json().catch(() => data.text());
};

const style = { height: '100vh' };

const App = () => {
const [query, setQuery] = React.useState('');
const explorerPlugin = useExplorerPlugin({
Expand All @@ -62,28 +81,18 @@ const App = () => {
snippets,
});

const plugins = React.useMemo(
() => [explorerPlugin, exporterPlugin],
[explorerPlugin, exporterPlugin],
);

return (
<GraphiQL
style={{ height: '100vh' }}
style={style}
query={query}
onEditQuery={setQuery}
plugins={[explorerPlugin, exporterPlugin]}
fetcher={async (graphQLParams, options) => {
const data = await fetch(
'https://swapi-graphql.netlify.app/.netlify/functions/index',
{
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
...options.headers,
},
body: JSON.stringify(graphQLParams),
credentials: 'same-origin',
},
);
return data.json().catch(() => data.text());
}}
plugins={plugins}
fetcher={fetcher}
/>
);
};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"tsc": "tsc --build"
},
"devDependencies": {
"@arthurgeron/eslint-plugin-react-usememo": "^1.1.4",
"@babel/cli": "^7.21.0",
"@babel/core": "^7.21.0",
"@babel/plugin-proposal-class-properties": "^7.18.6",
Expand Down
Loading