react-hooks

Async useState leading to undefined element down the hierarchy

可紊 提交于 2021-02-18 14:49:28
问题 Setup/Scenario I am using twitter api to fetch data and rendering it in twitter card using react-native-socials. The tweet data ( JSON ) is stored and served through my backend and rendered in the app. I am using a Flatlist to render this data. Problem It's working fine for the first 3 loads, I am using pullToRefresh in flatlist to do a network call to get new data. But when I try the 4th time it gets an undefined element. I've replaced the Twitter element ( from react-native-socials lib )

How to create dynamic refs in functional component- Using useRef Hook

蹲街弑〆低调 提交于 2021-02-18 13:52:55
问题 I want to have refs in functional component which have elements dynamically rendered. please help me in creating Dynamic Refs using useRef Hook and accessing in the handler. 1) Need to create 3 useRefs to point 3 buttons. 2) Access them in button handler using "ref1.value" Or "ref2.value" etc let abc=[1,2,3]; function submitclick(){ alert(123); // Here i want to access the buttons with using the refs } return ( <div> { abc.map(function(index){ return (<button ref='123' onClick={submitclick}>{

How to create dynamic refs in functional component- Using useRef Hook

我是研究僧i 提交于 2021-02-18 13:51:31
问题 I want to have refs in functional component which have elements dynamically rendered. please help me in creating Dynamic Refs using useRef Hook and accessing in the handler. 1) Need to create 3 useRefs to point 3 buttons. 2) Access them in button handler using "ref1.value" Or "ref2.value" etc let abc=[1,2,3]; function submitclick(){ alert(123); // Here i want to access the buttons with using the refs } return ( <div> { abc.map(function(index){ return (<button ref='123' onClick={submitclick}>{

React Hooks: What is the difference between 'useMutationEffect' and 'useLayoutEffect'?

一曲冷凌霜 提交于 2021-02-18 10:30:27
问题 What is the difference between useMutationEffect and useLayoutEffect in term of usage? I have read all available material online including (but not limited to) Hooks Reference Blog post by Kent Difference between useEffect and other two hooks is clear. but difference between useMutationEffect and useLayoutEffect is still not clear. I know the order of execution is: useMutationEffect useLayoutEffect useEffect 回答1: First, you have to understand the different phases of Rendering. A DOM mutation

What is the expected return of `useEffect` used for?

守給你的承諾、 提交于 2021-02-18 06:28:29
问题 In the React documentation for hooks they say: "This also allows you to handle out-of-order responses with a local variable inside the effect" useEffect(() => { let ignore = false; async function fetchProduct() { const response = await fetch('http://myapi/product/' + productId); const json = await response.json(); if (!ignore) setProduct(json); } fetchProduct(); return () => { ignore = true }; }, [productId]); Demo app Please help me understand this better by explaining: Why is the return a

What is the expected return of `useEffect` used for?

半世苍凉 提交于 2021-02-18 06:27:49
问题 In the React documentation for hooks they say: "This also allows you to handle out-of-order responses with a local variable inside the effect" useEffect(() => { let ignore = false; async function fetchProduct() { const response = await fetch('http://myapi/product/' + productId); const json = await response.json(); if (!ignore) setProduct(json); } fetchProduct(); return () => { ignore = true }; }, [productId]); Demo app Please help me understand this better by explaining: Why is the return a

What is the expected return of `useEffect` used for?

喜你入骨 提交于 2021-02-18 06:27:24
问题 In the React documentation for hooks they say: "This also allows you to handle out-of-order responses with a local variable inside the effect" useEffect(() => { let ignore = false; async function fetchProduct() { const response = await fetch('http://myapi/product/' + productId); const json = await response.json(); if (!ignore) setProduct(json); } fetchProduct(); return () => { ignore = true }; }, [productId]); Demo app Please help me understand this better by explaining: Why is the return a

What is the expected return of `useEffect` used for?

天涯浪子 提交于 2021-02-18 06:27:21
问题 In the React documentation for hooks they say: "This also allows you to handle out-of-order responses with a local variable inside the effect" useEffect(() => { let ignore = false; async function fetchProduct() { const response = await fetch('http://myapi/product/' + productId); const json = await response.json(); if (!ignore) setProduct(json); } fetchProduct(); return () => { ignore = true }; }, [productId]); Demo app Please help me understand this better by explaining: Why is the return a

What typescript type do I use with useRef() hook when setting current manually?

痴心易碎 提交于 2021-02-17 15:06:40
问题 How can I use a React ref as a mutable instance, with Typescript? The current property appears to be typed as read-only. I am using React + Typescript to develop a library that interacts with input fields that are NOT rendered by React. I want to capture a reference to the HTML element and then bind React events to it. const inputRef = useRef<HTMLInputElement>(); const { elementId, handler } = props; // Bind change handler on mount/ unmount useEffect(() => { inputRef.current = document

Apollo React - `useMutation` in ApolloClient setup?

给你一囗甜甜゛ 提交于 2021-02-17 06:12:14
问题 I have an interesting situation. I want to initiate refresh token request USING Apollo itself (a.k.a to call a mutation) Any idea, how to achieve something like this? export default new ApolloClient({ link: ApolloLink.from([ onError(({ graphQLErrors, networkError, operation, forward }) => { // useMutation(REFRESH_ACCESS_TOKEN) }), ]), new HttpLink({...}), }) Basically I'm just trying to useMutation(REFRESH_ACCESS_TOKEN) inside onError while creating new ApolloClient instance. The problem: