How to keep track of the object reference in a useEffect?

Advertisements I’m building some API hooks and I have trouble with deps property. I want to reinvoke the useEffect when content of deps changes, instead I’m running in a forever loop. deps can be either undefined or an any[] export const useReader = <TResponseData>({ url, deps, options }: TypeUseReader<TResponseData>) => { const [data, setData] =… Read More How to keep track of the object reference in a useEffect?

React memo and/or useCallback not working as expected

Advertisements So i have this Home Component, where there is a records state, i use it to perform a records.map() and return RecordItem components inside the table. function Home() { const [records, setRecords] = useState<Array<RecordType>>(loadRecords()); const removeRecord = useCallback((record: RecordType) => { setRecords(records => { const newRecords = records.filter(rec => rec !== record); localStorage.setItem(‘controle_financeiro’, JSON.stringify(newRecords));… Read More React memo and/or useCallback not working as expected

React – open new tab once

Advertisements If a component is rendered. I want to open a new tab. (window.open(url, "_blank")?.focus();) But (during development) this tab is opened twice because of the React.StrictMode. How can I prevent this from being called multiple times without disabling the StrictMode? My Attempts: function MyComp() { useMemo(() => window.open(url, "_blank")?.focus(), []); return <div>…</div>; } (doesn’t… Read More React – open new tab once

How to iterate through list elements in react?

Advertisements I am unable to figure out how to iterate through the rendered List in React Js and access the key and value properties. Code snippet const Cards = ({arr}) => { return ( <ul className=’output’> {arr.map(e => { return <li key={e.id} className="card" > {e.value} <FaTimes color=’red’ style={{cursor: ‘pointer’}} onClick={e => console.log(e.value)}/> </li> })} </ul>… Read More How to iterate through list elements in react?

React state not updating with new data being fetched?

Advertisements I have a useFetchPokemon hook that includes the following code: import { useState, useEffect } from "react"; export const useFetchPokemon = () => { const initialUrl = "https://pokeapi.co/api/v2/pokemon&quot; const [pokemon, setPokemon] = useState([]) as any; const [nextUrl, setNextUrl] = useState(null) const [previousUrl, setPreviousUrl] = useState(null) const getPokemonDetails = async (listOfPokemon: any) => { const… Read More React state not updating with new data being fetched?

Change headline text when clicking on multiple buttons in React

Advertisements I’m not sure how to tackle this problem. I have two or more buttons and whenever I click on a button, I want my text to change. Any ideas? import "./styles.css"; function App() { return ( <div className="App"> <h1>Change me!</h1> <button>Button 1</button> <button>Button 2</button> <button>Button 3</button> </div> ); } >Solution : This is a… Read More Change headline text when clicking on multiple buttons in React