I’m using multiple useEffect hooks to perform the componentDidMount and componentDidUpdate functionalities, however, looks like when component loads, all my useEffect fires initially… when my component mounts, I see all those console log there How could I only let my first useEffect fires, but the other three fires when the dependency changes only? Answer useEffect is not a direct replacement of componentDidMount and componentDidUpdate. Effect will run after each render, which is why you are seeing all those console logs. According to the React documentation, the second parameter of useEffect means you can choose to fire them (effects) only when
My website is too heavy because it downloads 200-400 images after fetching data from the server (Google’s Firebase Firestore). I came up with two solutions and I hope somebody answers one of them: I want to set each img to have a loading state and enable visitors to see the placeholder image until it is loaded. As I don’t know how many images I get until fetching data from the server, I find it hard to initialize image loading statuses by useState. Is this possible? Then, how? How can I Lazy load images? Images are initialized with a placeholder. When
I’ve been always using Redux, but I finally decided to try to play with Context API, but apparently I’m missing something. I get an error “TypeError: Object(…)(…) is undefined” in App.js, weather and details appear to be undefined. Could you please check out my code in order to help me out in finding a mistake? I thought I should have a direct access to the state without the need of destructuring. Or maybe destructuring is the only way to go? Here you can see all parts of my code – context + wrapped app.js. MyContext.js App.js Everything is wrapped in
i have component with text. It changed component own state with mouse click. But i want to save possibility to select and copy in by long click. Is there way to make it ? Selection is reset after rerender component. Code for example: Answer How about using onMouseDown and onMouseUp events yourself and calculate the time the user took to click instead of using onClick? You could for example do something like this: You can find a quick codesandbox as a demo here
Is it not allowed to use hooks inside of a Higher Order Component? When I try to do it with this simple pattern I’m getting the error Invalid hook call. Hooks can only be called inside of the body of a function component. Answer I believe you should use the hooks inside the HOC:
I am trying to rewrite class components to functions. Often, I have an enhanced component as class property: When writing function, I have to move the enhancing outside of function body, or it will get re-mounted on each render. Table is an external Component (devexpressGrid), but I suppose it does something like this: Is there a way, to still pass a prop from Grid to tableCell? This prop is not coming from any redux store, it is given when rendering Grid, like this: You can see the differences here: https://codesandbox.io/s/w2y76w53ww?fontsize=14 Answer The problem is that Table treats tableCell function as
I am trying to learn hooks and the useState method has made me confused. I am assigning an initial value to a state in the form of an array. The set method in useState is not working for me even with spread(…) or without spread operator. I have made an API on another PC that I am calling and fetching the data which I want to set into the state. Here is my code: The setMovies(result) as well as setMovies(…result) is not working. Could use some help here. I expect the result variable to be pushed into the movies array.