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

React – Using render props with conditional components causes hooks error

Advertisements Given this minimal example: import React, { useState } from ‘react’; function Component({ num }) { const [lorem, setLorem] = useState(‘lorem’); return ( <div> {num}: {lorem} </div> ); } function AnotherComponent({ render = Component }) { const [isOpen, setIsOpen] = useState(false); return ( <div> <button type="button" onClick={() => setIsOpen(true)}> Open </button> {isOpen ? render({… Read More React – Using render props with conditional components causes hooks error

How can I fetch data from API using an ID passed to a function when clicking a button

Advertisements I have used hooks in index.js to fetch data from an API. The data is displayed in rows and every row has a button with onClick={() => EditExercise(item.idexercise)}. EditExercise is a function in a .jsx file that receives the id and fetches a single item from the same API. function EditExercise(id){ const [error, setError]… Read More How can I fetch data from API using an ID passed to a function when clicking a button

How to rerender component, using useEffect and useState?

Advertisements I’m trying to show a table in my react component, but i don’t know how to use useEffect and useState to render table in first render. My component js: import React, { useEffect, useState } from ‘react’ const Projects = () => { const [tableData, setTableData] = React.useState() useEffect(() => { fetch(‘https://script.google.com/macros/s/AKfycbzuKuSOUfd9IOsQobix0PnIRv28IB3uTx5KdpVoVeNe04g7QzXQeeCEbsHBxE5CYdEV8A/exec&#8217;) //получение информации… Read More How to rerender component, using useEffect and useState?

How can I make regex pattern with characters, numbers, white space, double quotes, ?, !, commas, hyphen, dots, @ and &

Advertisements How can I make regex pattern allowing following characters, numbers, white space, double quotes, ?, !, commas, hyphen, dots, @ and & <textarea id="comments" type="textarea" placeholder=’comments’ {…register("comments", { required: true, minLength: { value: 5, message: "Minimum length of 5 letters" }, pattern: { value: /^[a-z0-9]+$/i, message: "Supports characters, numbers, white space, "", ?, !,… Read More How can I make regex pattern with characters, numbers, white space, double quotes, ?, !, commas, hyphen, dots, @ and &