Questions tagged [react-lifecycle]

313 questions
108
votes
4 answers

componentWillUnmount with React useEffect hook

How can the useEffect hook (or any other hook for that matter) be used to replicate componentWillUnmount? In a traditional class component I would do something like this: class Effect extends React.PureComponent { componentDidMount() {…
DAG
  • 6,710
  • 4
  • 39
  • 63
22
votes
2 answers

Can't call setState (or forceUpdate) on an unmounted component

I'm trying to fetch the data from the server after component has been updated but I couldn't manage to do that. As far as I understand componentWillUnmount is called when component is about to be destroyed, but I never need to destroy it so it's…
Nikola Stojaković
  • 2,257
  • 4
  • 27
  • 49
22
votes
2 answers

How in React native destroy an component instance to avoid memory leak and release underlie views objects of native code?

I want to destroy some component that i have instantiated to release memory. In my current app almost every view that i instantiate and then release it (remove reference to it) doesn't get garbage collected. I keep no reference to to views. I'm…
Hamid Karimi
  • 615
  • 1
  • 8
  • 12
14
votes
2 answers

Execute a function after Props change

I want to execute a function in Child Component after props gets changed in the Parent Component without having to manage an extra state in Child/Parent. {}} …
7
votes
1 answer

is it safe to use react js state to store token of authenticated user?

I am implementing a simple user authentication system using react js and node js api. This is what i am doing inside ComponentWillMount method :- 1.checking if the token exits (in localStorage) 2.if it doesn't exits then the value of state 'token'…
7
votes
5 answers

Props updated, componentDidUpdate did not fire

Is there ever a situation where componentDidUpdate would NOT fire, even if the props updated in React?
J Seabolt
  • 2,576
  • 5
  • 25
  • 57
7
votes
2 answers

React app componentDidMount not getting props from parent

I'm trying to pass props from a parent component to a child component and even though its getting called twice (dunno why, componentDidMount should get called only once) the props seem to be empty. Parent component: class Members extends Component{ …
Dieguinho
  • 758
  • 2
  • 14
  • 31
6
votes
1 answer

Ways to fix a child component that is not re-rendering (due to change in data being passed in as props, not state)?

Background I am working on a Meteor app that uses ReactJS as the rendering library. Currently, I'm having trouble with having a Child component re-render when data has been updated, even though the parent is accessing the updated data & supposedly…
5
votes
1 answer

What is different between snapshot and prevState, prevProps from componentDidUpdate in react?

I just started to studying React. When learning LifeCycle and I wonder about componentDidUpdate method. As I read componentDidUpdate method can have three params (prevProps, prevState, snapshot). If I can find out prevState from componentDidUpdate…
EUNJI JO
  • 63
  • 5
5
votes
5 answers

Call api before first render in functional component in React.js

If I want to call API after the first rendering of component, I know we have useEffect hook to call the API method. (I am talking about functional components only. No class component). Is there any way, I can call the API before my component renders…
5
votes
2 answers

How can i ignore react warnings? (Not Native app)

I get this warring in my react app. It says componentWillReceiveProps has been renamed... But I don't have "componentWillReceiveProps" in my code only effects ... Maybe it is inside in node modules. So i trying to ignore them But i don't know…
MINJA KIM
  • 876
  • 1
  • 8
  • 22
5
votes
3 answers

Using React Hooks, why are my event handlers firing with the incorrect state?

I'm trying to create a copy of this spinning div example using react hooks. https://codesandbox.io/s/XDjY28XoV Here's my code so far import React, { useState, useEffect, useCallback } from 'react'; const App = () => { const [box, setBox] =…
5
votes
2 answers

What does nextState do in shouldComponentUpdate?

In the React lifecycle function shouldComponentUpdate(nextProps, nextState), nextProps is self explanatory. But what does nextState do? It doesn't sound right that I can evaluate upcoming state before even deciding if the component should be…
Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232
5
votes
0 answers

Stop componentWillUnmount

I am working on a react app where I want to stop my component from unmounting based on some conditions. As far as I have researched I found one option of setRouteLeaveHook using react router (as mentioned here). But the problem is I have same route…
Shivani
  • 350
  • 3
  • 13
5
votes
1 answer

Clearing specific redux state before rendering component

I have the following "Buy" button for a shopping cart. I also have a component called Tooltip, which will display itself for error/success messages. It uses the button's width to determine it's centre point. Hence, I use a `ref since I need to…
Gurnzbot
  • 3,742
  • 7
  • 36
  • 55
1
2 3
20 21