react-hooks

React State is not updated with socket.io

半腔热情 提交于 2021-02-11 12:40:29
问题 When page loaded first time, I need to get all information, that is why I am calling a fetch request and set results into State [singleCall function doing that work] Along with that, I am connecting websocket using socket.io and listening to two events (odds_insert_one_two, odds_update_one_two), When I got notify event, I have to check with previous state and modify some content and update the state without calling again fetch request. But that previous state is still [] (Initial). How to get

React State is not updated with socket.io

本小妞迷上赌 提交于 2021-02-11 12:39:11
问题 When page loaded first time, I need to get all information, that is why I am calling a fetch request and set results into State [singleCall function doing that work] Along with that, I am connecting websocket using socket.io and listening to two events (odds_insert_one_two, odds_update_one_two), When I got notify event, I have to check with previous state and modify some content and update the state without calling again fetch request. But that previous state is still [] (Initial). How to get

How to trigger useEffect() with multiple dependencies only on button click and on nothing else

半腔热情 提交于 2021-02-11 12:28:51
问题 I have the following code: import React, { useState, useEffect } from "react" function callSearchApi(userName: string, searchOptions: SearchOptions, searchQuery: string): Promise<SearchResult>{ return new Promise((resolve, reject) => { const searchResult = searchOptions.fooOption ? ["Foo 1", "Foo 2", "Foo 3"] : ["Bar 1", "Bar 2"] setTimeout(()=>resolve(searchResult), 3000) }) } type SearchOptions = { fooOption: boolean } type SearchResult = string[] export type SearchPageProps = { userName:

How to use useState to update Data?

霸气de小男生 提交于 2021-02-11 12:23:04
问题 I am building a multistep Form and I am trying to update my Data with the useState-Hook. Unfortunatly I do no how to pass the components of the hook as a prop, to update them, after performing an onClick Event Thats the important part of my code multisteform.js const defualtData = { Grundstück:'', Haus:"Haus", Wohnung:"Wohnung", Gewerbe:"Gewerbe", } const steps = [ {id: 'Häuser'}, {id: 'Fläche'}, {id: 'Grundstückerschlosse'}, {id: 'Bebauungsmöglichkeiten'} ] export const MultiStepFrom = () =>

Work around to use custom hook in useEffect?

折月煮酒 提交于 2021-02-11 12:20:53
问题 I have a custom hook called Api which fetches data from my API & handles my auth token and refresh tokens. On my Main app, there are multiple ways that my state variable "postId" will be changed. And whenever it is changed, I want my API to fetch the new content for that. But I can't call my custom Api within useEffect, which is how I'm detecting changes in postId. Can someone please suggest a workaround? I spent forever making this API, now I feel like I can't even use it. Main.tsx: import

keep a variable type when I get value from input onchange with React-hooks

限于喜欢 提交于 2021-02-11 08:25:25
问题 const { useState } = React; const App = () => { const [num, setNum] = useState(0); return ( <input type="number" value={num} onChange={(e)=>setNum(parseInt(e.target.value))} /> ) } ReactDOM.render(<App />, document.getElementById('root')); <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> <div id="root" /> I want to keep num 's type as Number , not String . but

keep a variable type when I get value from input onchange with React-hooks

情到浓时终转凉″ 提交于 2021-02-11 08:24:30
问题 const { useState } = React; const App = () => { const [num, setNum] = useState(0); return ( <input type="number" value={num} onChange={(e)=>setNum(parseInt(e.target.value))} /> ) } ReactDOM.render(<App />, document.getElementById('root')); <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> <div id="root" /> I want to keep num 's type as Number , not String . but

keep a variable type when I get value from input onchange with React-hooks

☆樱花仙子☆ 提交于 2021-02-11 08:21:28
问题 const { useState } = React; const App = () => { const [num, setNum] = useState(0); return ( <input type="number" value={num} onChange={(e)=>setNum(parseInt(e.target.value))} /> ) } ReactDOM.render(<App />, document.getElementById('root')); <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> <div id="root" /> I want to keep num 's type as Number , not String . but

Component local state not updating with react custom hooks

喜夏-厌秋 提交于 2021-02-11 07:16:04
问题 I'm just starting to use react hooks and I'm having some issues when using custom hooks. It's probably lack of understanding but here's what I'm attempting My Custom hook: import React, { useState } from "react" export const useValidateContent = initState => { const[valid, setValid] = useState(initState) const[errorMsg, setErrorMsg] = useState(null) const validate = () => { // Update component state to test setValid(false) setErrorMsg('Some error found') } return [valid, validate, errorMsg] }

How to render only subchild without rendering all high level component

狂风中的少年 提交于 2021-02-11 06:29:56
问题 I m having one sub child component which is inside a loop of parent component. when one of the sub child components is updating the state of parent component, it is re-rendering the all children since it is loop. How can i avoid the re-render for each iteration. It should update that particular sub child. import React, { useState } from "react"; function Parent() { const [selectedChild, setSelectedChild] = useState([]); const onChangeHandle = (event, id) => { const checked = event.target