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 #3124

Merged
merged 23 commits into from
May 27, 2023
Merged
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
10 changes: 9 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,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 @@ -310,7 +316,7 @@ module.exports = {
'@typescript-eslint/no-namespace': 'off',
},

plugins: ['promise', 'sonarjs', 'unicorn'],
plugins: ['promise', 'sonarjs', 'unicorn', '@arthurgeron/react-usememo'],
},
{
// Rules that requires type information
Expand Down Expand Up @@ -348,6 +354,7 @@ module.exports = {
rules: {
'jest/no-conditional-expect': 'off',
'jest/expect-expect': ['error', { assertFunctionNames: ['expect*'] }],
'@arthurgeron/react-usememo/require-usememo': 'off',
},
},
{
Expand Down Expand Up @@ -413,6 +420,7 @@ module.exports = {
'no-undef': 'off',
'react/jsx-no-undef': 'off',
'react-hooks/rules-of-hooks': 'off',
'@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