use-effect

Multiple times render in react functional component with hooks [duplicate]

馋奶兔 提交于 2020-07-21 10:24:24
问题 This question already has answers here : Does React batch state update functions when using hooks? (3 answers) Closed 9 months ago . Actually I am not getting the right point of this problem. So seeking help. I have this state full functional component. The thing I have noticed here is that when I fetch data using useEffect hook then I get the response properly. The problem is, when I execute console.log("ok") in the return statement it provides output multiple times in the console. The

Multiple times render in react functional component with hooks [duplicate]

岁酱吖の 提交于 2020-07-21 10:22:24
问题 This question already has answers here : Does React batch state update functions when using hooks? (3 answers) Closed 9 months ago . Actually I am not getting the right point of this problem. So seeking help. I have this state full functional component. The thing I have noticed here is that when I fetch data using useEffect hook then I get the response properly. The problem is, when I execute console.log("ok") in the return statement it provides output multiple times in the console. The

React Hook “useState” is called in function “setResults” which is neither a React function component or a custom React Hook function

自古美人都是妖i 提交于 2020-07-18 06:15:06
问题 I am trying to make an API call in a functional component which is a react-hook and based on the result, re-render the component's content. Here is the code: Component which is calling the API- function IntegrationDownshift() { render( <Paper square> {setResults(inputValue).map( (suggestion, index) => renderSuggestion({ suggestion, index, itemProps: getItemProps({ item: suggestion.userFullName }), highlightedIndex, selectedItem }) )} </Paper> ); } Here is the setResults function: function

Map data using useRef Hook

和自甴很熟 提交于 2020-07-10 07:31:36
问题 I am following this: Merge two JSON data into one with particular key values Now, I added an useEffect() hook. How can I make use of useRef to store my result? This is an asynchronous function. const App = (props) =>{ let result = useRef () useEffect(()=>{ return()=> (result.current = [courtdata, balldata].reduce((p, q) => p.map((c, i) => Object.assign({}, {[c.court_id] : q[i].color})))) }.[someTrigger]) return( <ul> {result.current.map(r => ( <li> Court id - {r.court_id} | Ball colour - {r

Map data using useRef Hook

核能气质少年 提交于 2020-07-10 07:31:12
问题 I am following this: Merge two JSON data into one with particular key values Now, I added an useEffect() hook. How can I make use of useRef to store my result? This is an asynchronous function. const App = (props) =>{ let result = useRef () useEffect(()=>{ return()=> (result.current = [courtdata, balldata].reduce((p, q) => p.map((c, i) => Object.assign({}, {[c.court_id] : q[i].color})))) }.[someTrigger]) return( <ul> {result.current.map(r => ( <li> Court id - {r.court_id} | Ball colour - {r

Why can't useEffect access my state variable in a return statement?

偶尔善良 提交于 2020-06-29 04:27:09
问题 I don't understand why my useEffect() React function can't access my Component's state variable. I'm trying to create a log when a user abandons creating a listing in our app and navigates to another page. I'm using the useEffect() return method of replicating the componentWillUnmount() lifecycle method. Can you help? Code Sample let[progress, setProgress] = React.useState(0) ... user starts building their listing, causing progress to increment ... console.log(`progress outside useEffect: $

Getting error after i put Async function in useEffect

时光毁灭记忆、已成空白 提交于 2020-06-13 19:36:06
问题 In the useEffect function, If i just mention the getResults function variable, the app doesn't crash, but when i call it as i'm doing in code below, i get these errors : react-dom.development.js:21857 Uncaught TypeError: destroy is not a function and Consider adding an error boundary to your tree to customize error handling behavior. function App() { const [foods, setFoods] = useState([]); const [isLoaded, setIsLoaded] = useState(false); useEffect(() => getResponse()); const getResponse =

Getting error after i put Async function in useEffect

人盡茶涼 提交于 2020-06-13 19:35:52
问题 In the useEffect function, If i just mention the getResults function variable, the app doesn't crash, but when i call it as i'm doing in code below, i get these errors : react-dom.development.js:21857 Uncaught TypeError: destroy is not a function and Consider adding an error boundary to your tree to customize error handling behavior. function App() { const [foods, setFoods] = useState([]); const [isLoaded, setIsLoaded] = useState(false); useEffect(() => getResponse()); const getResponse =

Why is the cleanup function from `useEffect` called on every render?

非 Y 不嫁゛ 提交于 2020-06-11 17:49:57
问题 I've been learning React and I read that the function returned from useEffect is meant to do cleanup and React performs the cleanup when the component unmounts. So I experimented with it a bit but found in the following example that the function was called every time the component re-renders as opposed to only the time it got unmounted from the DOM, i.e. it console.log("unmount"); every time the component re-renders. Why is that? function Something({ setShow }) { const [array, setArray] =

reg ex works differently on two different files of code

巧了我就是萌 提交于 2020-05-17 07:46:46
问题 I am using a regEx expression to filter out the youtube ID of a youtube link. I am using it in two different spots - It works great in the first spot... but the second time I use the same expresion, with the same input, it does not work. Here are the two functions. First - Here is the one that works. The user inputs the data in a text input, and it returns match[2], which is the youtube ID at the end of the link. The user can copy paste in a whole paragraph of text containing a youtube link,