Questions tagged [react-lifecycle]
313 questions
3
votes
2 answers
React - a way to unify, if many components have the same piece of code in lifecycle methods
I have multiple component with similar piece code in lifecycle methods and some similarity in state variables. Is there a way to unify them, by inheriting from one parent or something like that?
constructor(props) {
super(props);
this.state…

Anna
- 2,911
- 6
- 29
- 42
3
votes
2 answers
How to create React's new static function getDerivedStateFromProps as a lifecycle method using an HoC in the recompose library?
Recently it has come out that soon React will be deprecating componentWillReceiveProps and in it's place is the new static function getDerivedStateFromProps. See more here
I am currently migrating my apps to this new API, but am having an issue…

theJuls
- 6,788
- 14
- 73
- 160
3
votes
4 answers
ComponentDidMount not getting called after redux state update?
I think I'm missing a concept here about React and Redux. I'm trying to work with objects stored in redux, and I'm having trouble.
REDUX:
I have an action fetchItems, that gets all items from the database. This action works successfully.
REACT:
I…

Asool
- 13,031
- 7
- 35
- 49
2
votes
1 answer
Console log in React function components for beginner
My problem
The code is simple. When you switch to any new button, it will log the updated sourceType, then "My Log".
You start off at the posts tab, then you clicked users tab, it will log "users" and "My log", which I expect.
Then if you click on…

free-stack
- 21
- 2
2
votes
1 answer
react hooks equivalent of passing a `key` to a component to control its lifecycle
If I have a component:
function MyComponent() {
const state = useState(resetState())
return jsx(state)
}
then I can trigger a state reset by passing a key to MyComponent:

bebbi
- 2,489
- 3
- 22
- 36
2
votes
3 answers
componentWillUnmount lifecycle with react useeffect hook
Is there any difference with these 3 useEffects
useEffect(() => {
return console.log('useEffect with undefined dep');
});
useEffect(() => {
return console.log('useEffect with empty array dep');
}, []);
useEffect(() => {
return…

Abdullah KOCAKAYA
- 71
- 4
2
votes
1 answer
I want to change state of the component without reloading metro in react-native
so basically i have bunch of apps in my application
as you can see in image
In my app it check if application is installed or not, if its installed its shows open and if not it shows install now the issue i'm facing is if application is not…

Siddharth Khengare
- 23
- 2
2
votes
1 answer
Calling setState inside componentDidMount synchronously
I am beginner in react and I am learning about lifecycle hooks. I came across some articles stating don't call setState synchronously inside componentDidMount. But we can setState in then block of promise.
The reason mentioned was setState will…

Ayush Mishra
- 267
- 3
- 14
2
votes
0 answers
renderChildren() functional component equivalent
I'm maintaining React app and I have to turn all class components into functional.
One component has the renderChildren() method.
Is there a way to mimic that method in a functional component with hooks?
I really appreciate the support.

Jose Maselli
- 23
- 2
2
votes
4 answers
React setState inside componentDidUpdare leads to Maximum update depth exceeded
I am getting
Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.
But what I read I…

shane_00
- 101
- 1
- 2
- 9
2
votes
4 answers
How to deal with asynchronous props and state
I can't quite wrap my head around this.
I'm having to pass data that's fetched asynchronously. Issue is, the props are asynchronous as well. Here's a simplified version of the component:
import React, { Component } from 'react'
import CSVLink from…

Samuel Hulla
- 6,617
- 7
- 36
- 70
2
votes
2 answers
Call Function On Mount in ReactJS
I have a problem calling a function when a component loads on ReactJS. I try to use componentDidMount() and has error compiling. Pls check my code below. Thanks
export default function Customers() {
const classes = useStyles();
const…

Joseph
- 7,042
- 23
- 83
- 181
2
votes
2 answers
Returning null when accessing object properties in state?
In my React.js app, I am fetching a quote from API and storing it in the state object, When trying to access the properties of the object which is stored in state. Returning null.
Code:
import React, { Component } from "react";
export default class…

Nawaz Mujawar
- 23
- 6
2
votes
1 answer
Remove items from the database on leaving or closing the page from a React Component
I have a React component that is part of a React/Apollo Booking app. I store all the bookings in a SQL database that is hooked up with React-Apollo in between. Before the customer starts the payment process the database is sent temporary bookings to…

Sebastian Berg
- 69
- 1
- 11
2
votes
0 answers
React triggering full life-cycle when props children submitted outside render method
I have a situation where I need the components of my application to render in one of two states based on a user click of a button. The problem I am having is that React triggers the full life-cycle of these components, even though all I am changing…

Nate
- 95
- 7