react-hooks

Set state with same value using hooks will cause a rerender?

感情迁移 提交于 2021-01-29 18:48:41
问题 Using hooks, If I call setState with the same value as the state , will it rerender the component? If yes, how can I avoid that? e.g. const [state, setState] = useState(foo) ... // any where in the code setState(foo) Considering that foo can be any thing, such as {} , true , props.bar or a variable from out side of the component (constant). 回答1: It won't re-render the component if you call setState with the same value. Try this out: import React, { useState, useEffect } from "react"; const

Using transitions in React Hooks and D3

一个人想着一个人 提交于 2021-01-29 17:22:37
问题 I am trying to replicate the gauge seen here in React with Hooks. It's the first time I am working with D3.js and even though I managed to replicate the gauge itself, the behavior is a bit strange (transition of the needles does not work) and I am not fully understanding the dynamics between useEffect and D3. Basically I want to add two needles that will each receive different values and use a transition when their value changes. //declaring angle value const [angle1, setAngle1] = useState(0)

Reactjs hooks: update value of map

五迷三道 提交于 2021-01-29 14:26:19
问题 I'm new to reactjs and that's why it comes naive to you. I need to update the value of a map in which its keys are unknown. const App = () => { const [storeMap, setStoreMap] = useState(new Map()); let _tmpMap = new Map(); return (<> {Object.keys({ key1: "hey", key2: "you" }).map((item) => { return ( <button value={item} key={item} onClick={(e) => { _tmpMap.set(item, e.target.value); console.log(..._tmpMap); // {1} setStoreMap(_tmpMap); }} > {item} </button> ); // return <i key={item}>KJ </i>;

Filtering JSON results in categories - React Hooks

痴心易碎 提交于 2021-01-29 12:49:28
问题 Instructions Create a web interface that allows a user: To filter a list of players based on specific criteria. These are the search filters you will be creating based on the test data: Age: allow the user to filter for players within a specific age or age range (e.g. 7, 5-9, 15-17, etc.) Gender: allow the user to filter for players based on their specified gender State: allow the user to filter for players based on the state they reside in from available test data Status: allow the user to

Arrow functions only have access to initial state created by useReducer, not updated state

删除回忆录丶 提交于 2021-01-29 11:14:46
问题 I am trying to access the updated state from useReducer inside of an arrow function in a functional component. However, when I call the state, I'm only getting the initial state object. The reducer function const reducer = (state, action) => { switch (action.type) { case 'UPDATE': return { ...state, [action.progress.request]: action.progress } case 'REMOVE': const { [action.progress.request]: value, ...rest } = state return rest default: return state } } The react component const

Redux-form: update syncErrors cause form to skip modifications

青春壹個敷衍的年華 提交于 2021-01-29 10:51:47
问题 I have a checkbox in my redux-form and upon pressing the checkbox, it shows for a millisecond as if it unchecked it but then returned to previous checked state and only clicking checkbox the second time actually unchecks it. I looked into redux-dev-tools and it shows that after form CHANGE event, UPDATE_SYNC_ERRORS one follows. Here is some code that I have: const ProjectFamiliesFilterInput = React.memo(({ familyOptions, analysisGroupOptions, projectAnalysisGroupsByGuid, value, onChange, ..

React Hooks: state not updating when called inside Socket.io handler

孤街浪徒 提交于 2021-01-29 10:26:43
问题 const [questionIndex, setQuestionIndex] = useState(0); ... socket.on('next', () => { console.log('hey') setQuestionIndex(questionIndex + 1); }); ... useEffect(() => { console.log(questionIndex); }, [questionIndex]); I have a page that connects to a websocket using Socket.io. I am attempting to update the value of my questionIndex state variable when I receive a 'next' message from the socket. I seem to be receiving the message because 'hey' is printed, but questionIndex only updates after the

How to initialise the set function of useState for TypeScript in a createContext?

自闭症网瘾萝莉.ら 提交于 2021-01-29 09:46:24
问题 I have a Provider that provides a state variable and its corresponding setter through two contexts . const BookedBatchContext = createContext({}) const SetBookedBatchContext = createContext(null) const initialState = { id: null } The Provider looks like this: export const BookedBatchProvider = ({ children }: { children: any }) => { const [bookedBatch, setBookedBatch] = useState(localState ||initialState) return ( <SetBookedBatchContext.Provider value={setBookedBatch}> <BookedBatchContext

react material-ui textfield getting error: invalid hook call

℡╲_俬逩灬. 提交于 2021-01-29 09:14:31
问题 I have this simple code to show the text field: I get the error Error in /turbo_modules/react@16.13.0/cjs/react.development.js (1465:13) Invalid hook call. The code is at this link https://stackblitz.com/edit/react-6dgvfj?file=UserForm.js import React, { Component } from 'react'; import compose from 'recompose/compose'; import { connect } from 'react-redux'; import TextField from '@material-ui/core/TextField'; const UserForm = props => { return ( <div> This is userform component. <TextField

changing multiple states in react js useState() hook

老子叫甜甜 提交于 2021-01-29 08:50:49
问题 So I was wondering what actually happens when I change more than one state in a handler function. Will they both get updated simultaneously or they will be executed one after another . const [x, setX] = useState(0) const [y, setY] = useState(0) const handlerFunction = () => { setX(x+1) setY(y+1) } Also what If one state depend on others? const handlerFunction = () => { setX(x+1) setY(x+1) } or what if const [x, setX] = useState(0) const [y, setY] = useState(x) const handlerFunction = () => {