use-state

How To Solve The React Hook Closure Issue?

匆匆过客 提交于 2021-01-27 12:07:29
问题 import React, { useState} from "react"; import ReactDOM from "react-dom"; function App() { const [count, setCount] = useState(0); function handleAlertClick(){ return (setTimeout(() => { alert("You clicked on: " + count); }, 3000)) } return ( <div> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}>Click me</button> <button onClick={handleAlertClick}>Show alert</button> </div> ); } I just want to know if this works the way that I think it does, or if there is a better

setInterval updating state in React but not recognizing when time is 0

岁酱吖の 提交于 2021-01-27 06:51:22
问题 I am practicing React useState hooks to make a quiz timer that resets every ten seconds. What I have now is updating the state each second, and the p tag renders accordingly. But when I console.log(seconds) it shows 10 every time, and so the condition (seconds === 0) is never met . In Chrome's React DevTools, the state is updating accordingly as well. What am I doing wrong here? import React, {useState } from 'react'; function App() { const [seconds, setSeconds] = useState(10); const

setInterval updating state in React but not recognizing when time is 0

放肆的年华 提交于 2021-01-27 06:50:24
问题 I am practicing React useState hooks to make a quiz timer that resets every ten seconds. What I have now is updating the state each second, and the p tag renders accordingly. But when I console.log(seconds) it shows 10 every time, and so the condition (seconds === 0) is never met . In Chrome's React DevTools, the state is updating accordingly as well. What am I doing wrong here? import React, {useState } from 'react'; function App() { const [seconds, setSeconds] = useState(10); const

How can I access React state in my eventHandler?

妖精的绣舞 提交于 2021-01-24 07:23:24
问题 This is my state: const [markers, setMarkers] = useState([]) I initialise a Leaflet map in a useEffect hook. It has a click eventHandler. useEffect(() => { map.current = Leaflet.map('mapid').setView([46.378333, 13.836667], 12) . . . map.current.on('click', onMapClick) }, [] Inside that onMapClick I create a marker on the map and add it to the state: const onMapClick = useCallback((event) => { console.log('onMapClick markers', markers) const marker = Leaflet.marker(event.latlng, { draggable:

How can I access React state in my eventHandler?

久未见 提交于 2021-01-24 07:22:44
问题 This is my state: const [markers, setMarkers] = useState([]) I initialise a Leaflet map in a useEffect hook. It has a click eventHandler. useEffect(() => { map.current = Leaflet.map('mapid').setView([46.378333, 13.836667], 12) . . . map.current.on('click', onMapClick) }, [] Inside that onMapClick I create a marker on the map and add it to the state: const onMapClick = useCallback((event) => { console.log('onMapClick markers', markers) const marker = Leaflet.marker(event.latlng, { draggable:

How can I access React state in my eventHandler?

半世苍凉 提交于 2021-01-24 07:22:36
问题 This is my state: const [markers, setMarkers] = useState([]) I initialise a Leaflet map in a useEffect hook. It has a click eventHandler. useEffect(() => { map.current = Leaflet.map('mapid').setView([46.378333, 13.836667], 12) . . . map.current.on('click', onMapClick) }, [] Inside that onMapClick I create a marker on the map and add it to the state: const onMapClick = useCallback((event) => { console.log('onMapClick markers', markers) const marker = Leaflet.marker(event.latlng, { draggable:

Why is my react component rendering twice on initial load?

ぐ巨炮叔叔 提交于 2021-01-20 07:26:07
问题 I have a functional component called (First) function First() { const [count,setCount]=useState(0) console.log("component first rendering") // this logging is happening twice return ( <div> first component </div> ) } when i initially run the application the console statement is logging twice why is it, It should have been logged only once, because i haven't explicitily updated the state. 回答1: I've tried this out in code sandbox here and sure enough, it did render twice. This is because, in

Why is my react component rendering twice on initial load?

拥有回忆 提交于 2021-01-20 07:25:12
问题 I have a functional component called (First) function First() { const [count,setCount]=useState(0) console.log("component first rendering") // this logging is happening twice return ( <div> first component </div> ) } when i initially run the application the console statement is logging twice why is it, It should have been logged only once, because i haven't explicitily updated the state. 回答1: I've tried this out in code sandbox here and sure enough, it did render twice. This is because, in

React - using state to update values, or not

烂漫一生 提交于 2020-12-15 04:54:45
问题 I have a form with two columns. The left column is a proposed value for the right column. I currently present both values from separate API calls. This is working. This is the current code: const dbContact = useDbContact(contact.Group); This sets the value of the current values, contact.Group from the proposed values being passed to the API call. I then later use this to call the form input value: <Form.Row> <Form.Group as={Col} controlId="currentSecName" className="mb-2"> <Form.Control type=

Getting empty data from state when using useState React hook

♀尐吖头ヾ 提交于 2020-11-29 21:45:51
问题 I've stumbled upon an issue with the useState scope. I'm trying to create a dynamic Bootstrap Form where the user will be able to add more rows to the group (those rows will containt energy-related data). So, when I add 2+ rows and start changing inputs' values, enterEnvironmentData function fires empty data (last console.log). Where is my mistake? Why state data ( groupsData.group0.rows array ) is empty? const { useState, useEffect } = React; const App = () => { const [ defaultData, ] =