-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(react): reduce over-fetching between renders #1594
- Loading branch information
Showing
26 changed files
with
193 additions
and
97 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use client'; | ||
|
||
import type { Variables } from 'gqty'; | ||
import { useState, type FunctionComponent } from 'react'; | ||
import Button from '~/components/tailwindui/Button'; | ||
import type { Query } from '~/gqty'; | ||
import { useQuery } from '~/gqty/react'; | ||
import Avatar from './Avatar'; | ||
import Card from './Card'; | ||
import SmallText from './SmallText'; | ||
import Text from './Text'; | ||
|
||
export type Props = {}; | ||
|
||
const Component: FunctionComponent<Props> = () => { | ||
const [searchValue, setSearchValue] = useState<string>(); | ||
|
||
return ( | ||
<> | ||
<SelectBox onChange={setSearchValue} /> | ||
{searchValue && <Character id={searchValue} />} | ||
</> | ||
); | ||
}; | ||
|
||
const SelectBox: FunctionComponent<{ | ||
onChange?: (value: string) => void; | ||
}> = ({ onChange }) => { | ||
const [value, setValue] = useState<string>(); | ||
|
||
return ( | ||
<div className="flex gap-3"> | ||
<input | ||
type="number" | ||
defaultValue={value} | ||
onChange={(e) => setValue(e.target.value)} | ||
className="border border-gray-300 rounded-md px-3 py-2 w-full text-black" | ||
/> | ||
<Button | ||
onClick={() => { | ||
if (value) { | ||
onChange?.(value); | ||
} | ||
}} | ||
> | ||
Search | ||
</Button> | ||
</div> | ||
); | ||
}; | ||
|
||
const Character: FunctionComponent<Variables<Query['character']>> = (props) => { | ||
const character = useQuery().character(props); | ||
|
||
return ( | ||
<Card> | ||
<Avatar character={character} /> | ||
|
||
<div className="flex-1"> | ||
<Text>{character?.name}</Text> | ||
<SmallText>{character?.species}</SmallText> | ||
<SmallText>{character?.origin?.name}</SmallText> | ||
</div> | ||
</Card> | ||
); | ||
}; | ||
|
||
export default Component; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
'use client'; | ||
|
||
import type { Variables } from 'gqty'; | ||
import { useState, type FunctionComponent } from 'react'; | ||
import Button from '~/components/tailwindui/Button'; | ||
import type { Query } from '~/gqty'; | ||
import { useQuery } from '~/gqty/react'; | ||
import Avatar from './Avatar'; | ||
import Card from './Card'; | ||
import SmallText from './SmallText'; | ||
import Text from './Text'; | ||
|
||
export type Props = {}; | ||
|
||
const Component: FunctionComponent<Props> = () => { | ||
const [searchValue, setSearchValue] = useState<string>(); | ||
|
||
return ( | ||
<> | ||
<SearchBox onChange={setSearchValue} /> | ||
<Characters filter={{ name: searchValue }} /> | ||
</> | ||
); | ||
}; | ||
|
||
const SearchBox: FunctionComponent<{ | ||
onChange?: (value: string) => void; | ||
}> = ({ onChange }) => { | ||
const [inputName, setInputName] = useState(''); | ||
|
||
return ( | ||
<div className="flex gap-3"> | ||
<input | ||
type="text" | ||
defaultValue={inputName} | ||
onChange={(e) => setInputName(e.target.value)} | ||
className="border border-gray-300 rounded-md px-3 py-2 w-full text-black" | ||
/> | ||
<Button onClick={() => onChange?.(inputName)}>Search</Button> | ||
</div> | ||
); | ||
}; | ||
|
||
const Characters: FunctionComponent<Variables<Query['characters']>> = ( | ||
props | ||
) => { | ||
const query = useQuery(); | ||
|
||
return ( | ||
<> | ||
{query.characters(props)?.results?.map((character) => ( | ||
<Card key={character?.id ?? '0'}> | ||
<Avatar character={character} /> | ||
|
||
<div className="flex-1"> | ||
<Text>{character?.name}</Text> | ||
<SmallText>{character?.species}</SmallText> | ||
<SmallText>{character?.origin?.name}</SmallText> | ||
</div> | ||
</Card> | ||
))} | ||
</> | ||
); | ||
}; | ||
|
||
export default Component; |
4 changes: 2 additions & 2 deletions
4
examples/gnt/app/Character.tsx → examples/gnt/app/components/RscCharacter.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.