react-hooks

Fetching data with hooks

杀马特。学长 韩版系。学妹 提交于 2021-02-05 07:51:30
问题 I'm having a hard time to wrap my head around the best way to fetch data from an API only once (and when it's first requested) and then storing the result for reuse and/or other component functions. Below is a working example with useContext and useReducer , but it's quite complex for such an "easy" task. Is there a better approach? And what's the best way to implement this for a large amount of API calls. Any suggestions are greatly appreciated. import React from "react"; import ReactDOM

Fetching data with hooks

邮差的信 提交于 2021-02-05 07:51:08
问题 I'm having a hard time to wrap my head around the best way to fetch data from an API only once (and when it's first requested) and then storing the result for reuse and/or other component functions. Below is a working example with useContext and useReducer , but it's quite complex for such an "easy" task. Is there a better approach? And what's the best way to implement this for a large amount of API calls. Any suggestions are greatly appreciated. import React from "react"; import ReactDOM

How to use hooks in correct way?

妖精的绣舞 提交于 2021-02-05 07:48:28
问题 I have some issue 1- I defined a function that gets data from API and calling it in useEffect , It's work well But i got this warning in VScode. React Hook React.useEffect has a missing dependency: 'getOpenOrders'. Either include it or remove the dependency array. 2- I implement pagination in the FlatList, So when the user reached the end of data List I calling a function to increment the current page, and based on current page updated, getOpenOrders fetched again because i pass currentPage

Property 'value' does not exist on type 'never'. when use useRef hook in mui

梦想的初衷 提交于 2021-02-04 18:18:31
问题 I am using material UI to build a login and registration page, using useRef to return a TextFiled ref instance, and xxxRef.current.value to get the input value. I can smoothly run my project and can get the value correctly,but the console always reminded me that: Property 'value' does not exist on type 'never'. Here is my code snippets: const accountRef = useRef(); <TextField variant="outlined" margin="normal" required fullWidth id="account" label="Account" name="account" autoComplete=

React hook useEffect dependency array

懵懂的女人 提交于 2021-02-04 09:56:47
问题 I trying to wrap my head around the new hooks api of react. Specifically, I'm trying to construct the classic use case that once was the following: componentDidUpdate(prevProps) { if (prevProps.foo !== this.props.foo) { // animate dom elements here... this.animateSomething(this.ref, this.props.onAnimationComplete); } } Now, I tried to build the same with a function component and useEffect , but can't figure out how to do it. This is what I tried: useEffect(() => { animateSomething(ref, props

How to get input field value on link click?

白昼怎懂夜的黑 提交于 2021-01-29 21:20:16
问题 I am generating my form dynamically using json.but I struck at one place while getting value of input field on click. When My demo application run .it shows a input field ( where user enter a mobile number) and a button (text send OTP).After entering number example (9891234178) and press enter it shows OTP field and a link resend OTP . I want to capture click handler of link (Resend otp) on click I want to get my input field value (mobile number value). Can It is possible to get value ? Here

Refactoring class component to functional component with hooks, getting Uncaught TypeError: func.apply is not a function

夙愿已清 提交于 2021-01-29 20:37:31
问题 This is my first attempt to refactor code from a class component to a functional component using React hooks. The reason we're refactoring is that the component currently uses the soon-to-be-defunct componentWillReceiveProps lifecylcle method, and we haven't been able to make the other lifecycle methods work the way we want. For background, the original component had the aforementioned cWRP lifecycle method, a handleChange function, was using connect and mapStateToProps, and is linking to a

React useState with sorted array does not cause re-render of SemanticUI table if initial sorting is done

馋奶兔 提交于 2021-01-29 20:19:49
问题 I have a table with SemanticUI that renders a list of companies and allows the list to be resorted based on clicking the header of the table. I thought I had this wrapped up working fine (the dev env is sending me a pre-sorted list), but when writing tests, I realized I cannot guarantee the list is pre-sorted. So I edited my initial useState logic to have a sort function which is the same logic for the header click: BEFORE (works): const [data, setData] = useState(companies); AFTER (does not

React hooks state is not updated right after it's set with async

心不动则不痛 提交于 2021-01-29 19:56:44
问题 Is it not possible to get the updated hook value right after setting it? I assume react only updates value for the next render, since my screen following async-await renders fine. Just logs are not printing out the state I was expecting. Not sure if I'm suppose to log state like this, or write another useEffect just for the states I need and put console.log in there instead? const [uid, setUID] = useState<string | undefined>(undefined); const [state, setState] = useState<{ [key: string]: any;

Render a functional component after fetching data from a POST API?

爷,独闯天下 提交于 2021-01-29 19:52:40
问题 I want to re-render my component after the data fetched from my API. I used hooks to handle that. but it does not working. actually my component renders for the first time, not after fetching data! I have an external function. By this function, I can give a province name to that and then receive a list of the cities which they are in that province. my function is like below: const axios = require('axios'); export default async function getCities(state) { let res = [], err; try { await axios