Skip to content

Instantly share code, notes, and snippets.

View kivircik-parantez's full-sized avatar
🎯
Focusing

Zeynep Yurt Şimşek kivircik-parantez

🎯
Focusing
View GitHub Profile
@kivircik-parantez
kivircik-parantez / use-rtk-query-hooks.ts
Created January 8, 2023 21:10
Using RTK Query Hooks in Components
import * as React from 'react'
import { useGetPokemonByNameQuery } from './services/pokemon'
export default function App() {
// Using a query hook automatically fetches data and returns query values
const { data, error, isLoading } = useGetPokemonByNameQuery('bulbasaur')
// Individual hooks are also accessible under the generated endpoints:
// const { data, error, isLoading } = pokemonApi.endpoints.getPokemonByName.useQuery('bulbasaur')
// render UI based on data and loading state
import { configureStore } from '@reduxjs/toolkit'
// Or from '@reduxjs/toolkit/query/react'
import { setupListeners } from '@reduxjs/toolkit/query'
import { pokemonApi } from './services/pokemon'
export const store = configureStore({
reducer: {
// Add the generated reducer as a specific top-level slice
[pokemonApi.reducerPath]: pokemonApi.reducer,
},
@kivircik-parantez
kivircik-parantez / createApi.ts
Created January 8, 2023 17:58
Create an API Slice
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
import type { Pokemon } from './types'
// Define a service using a base URL and expected endpoints
export const pokemonApi = createApi({
reducerPath: 'pokemonApi',
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
endpoints: (builder) => ({
getPokemonByName: builder.query<Pokemon, string>({
query: (name) => `pokemon/${name}`,
@kivircik-parantez
kivircik-parantez / data-fetch-custom-hook.ts
Created January 1, 2023 12:17
Data Fetch Custom Hook
function SearchResults({ query }) {
const [page, setPage] = useState(1);
const params = new URLSearchParams({ query, page });
const results = useData(`/api/search?${params}`);
function handleNextPageClick() {
setPage(page + 1);
}
// ...
}
@kivircik-parantez
kivircik-parantez / fetch-data-clean-up.ts
Created January 1, 2023 12:08
Fetch Data and Clean Steal Response
function SearchResults({ query }) {
const [results, setResults] = useState([]);
const [page, setPage] = useState(1);
useEffect(() => {
let ignore = false;
fetchResults(query, page).then(json => {
if (!ignore) {
setResults(json);
}
});
@kivircik-parantez
kivircik-parantez / subscribe-external-store.ts
Created December 30, 2022 23:34
Subscribing to an external store
function subscribe(callback) {
window.addEventListener('online', callback);
window.addEventListener('offline', callback);
return () => {
window.removeEventListener('online', callback);
window.removeEventListener('offline', callback);
};
}
function useOnlineStatus() {
@kivircik-parantez
kivircik-parantez / pass-data-down-to-child.ts
Created December 30, 2022 23:27
Pass Data Down to Child
function Parent() {
const data = useSomeAPI();
// ...
// ✅ Good: Passing data down to the child
return <Child data={data} />;
}
function Child({ data }) {
// ...
}
@kivircik-parantez
kivircik-parantez / component-fully-controlled-by-its-parent.ts
Created December 30, 2022 23:18
Component is fully controlled by its parent
// ✅ Also good: the component is fully controlled by its parent
function Toggle({ isOn, onChange }) {
function handleClick() {
onChange(!isOn);
}
function handleDragEnd(e) {
if (isCloserToRightEdge(e)) {
onChange(true);
} else {
@kivircik-parantez
kivircik-parantez / notify-parent-components-about-state-change.ts
Created December 30, 2022 23:07
Notifying parent components about state changes
function Toggle({ onChange }) {
const [isOn, setIsOn] = useState(false);
function updateToggle(nextIsOn) {
// ✅ Good: Perform all updates during the event that caused them
setIsOn(nextIsOn);
onChange(nextIsOn);
}
function handleClick() {
@kivircik-parantez
kivircik-parantez / only-runs-per-app-load.ts
Created December 30, 2022 22:56
Only runs once per app load
if (typeof window !== 'undefined') { // Check if we're running in the browser.
// ✅ Only runs once per app load
checkAuthToken();
loadDataFromLocalStorage();
}
function App() {
// ...
}