Linked Questions
29 questions linked to/from How to compare oldValues and newValues on React Hooks useEffect?
3
votes
4
answers
15k
views
React hooks: how to detect when particular state variable has updated [duplicate]
Before React hooks, I would use componentDidUpdate(prevProps, prevState), and if I wanted to execute given code only when this.state.a had updated, I'd do
if (prevState.a !== this.state.a) {
<...&...
0
votes
2
answers
2k
views
Is there any way that React component still keep the value from previous render? [duplicate]
I have a parent component, that render one child component several time. This child component is used in many different components. I need to have an array in this child component that keeps all the ...
208
votes
14
answers
293k
views
Detecting user leaving page with react-router
I want my ReactJS app to notify a user when navigating away from a specific page. Specifically a popup message that reminds him/her to do an action:
"Changes are saved, but not published yet. Do ...
24
votes
5
answers
20k
views
useEffect when all dependencies have changed?
Currently useEffect is fired when just one of the dependencies have changed.
How could I update it / use it to fire when all of the dependencies have changed?
16
votes
4
answers
7k
views
Run Effect hook only when both dependencies change
I have a React component that fetches data using the useEffect hook like so:
const cache = {key: "data-fetched-using-key"}
function Config({key, options}) {
const [data, setData] = useState();
...
7
votes
4
answers
11k
views
More advanced comparison in React's useEffect
I am looking for a way to perform more advanced comparison instead of the second parameter of the useEffect React hook.
Specifically, I am looking for something more like this:
useEffect(
() => ...
2
votes
4
answers
8k
views
prevstate in react hook
How do I use prevState with useEffect in a functional component? I was told to update class component to functional component and I'm stuck in the componentDidUpdate part where prevState was used
...
7
votes
2
answers
3k
views
How to compare values from react redux state in hooks
I have recently written a table component using hooks , and every time page loads there is an API call to backend, so meanwhile there is a loading Spinner will be shown until there is an response from ...
2
votes
2
answers
4k
views
How to make useEffect hook call the provided effect only if all the elements in inputs change?
I'm trying to convert UNSAFE_componentWillReceiveProps into a hook. Below is the logic for using CWRP.
UNSAFE_componentWillReceiveProps(nextProps){
if (this.props.val1 !== nextProps.val1 && ...
4
votes
1
answer
5k
views
Why does useDeferredValue not work as expected in React 18?
I'm trying to use useDeferredValue hook in React 18 to defer updating a part of the UI based on a state variable.
According to React documentation:
During the initial render, the returned deferred ...
2
votes
3
answers
2k
views
Why usePrevious returns undefined on first render - react hooks?
I want to store the previous value in a variable and use it in a function. Let's say if the current value is 9, the previous value is supposed to be 8 (like one less). The problem is that console.log(...
2
votes
2
answers
4k
views
Best practice for combining React useContext and one-time data load (React hooks)?
I have a form composed of several input components. The form data is shared and shareable across these sibling components via a React context and the React hook useContext.
I am stumbling on how to ...
0
votes
4
answers
3k
views
How can we use useEffect for multiple props value changes
How can we identify props changes in functional component?. Sample code here. Can you please help me to convert this componentDidUpdate method to functional componet using useEffect or any other way.
...
1
vote
3
answers
2k
views
How to correctly wait on state to update/render instead of using a delay/timeout function?
I will attempt to keep this brief, but I am not 100% sure of the correct method of achieving what I am aiming for. I have been thrown in the deep end with React with not much training, so I have most ...
4
votes
2
answers
1k
views
How to await a setState call finishing when using useState hook that immediately needs that state to make an API call?
I have a voice dictation custom hook, along with a separate custom hook that appends the dictation results to an object that stores "Note" values.
If the user clicks save too early, there are still ...