-
Notifications
You must be signed in to change notification settings - Fork 49.7k
Closed
Description
Do you want to request a feature or report a bug?
Feature.
What is the current behavior?
As discussed in #14830, once a prop is passed through the useState hook, it no longer acts on updates to that prop from the parent. The user must always pair the useState call with a useEffect call to restore the normal prop behaviour.
What is the desired behavior?
This is a common enough pattern that it might be worth adding a specific hook for this situation. Is that something your team would be up for?
This is the custom hook I've been using for this purpose:
import React, { useState, useEffect } from "react";
function useDynamicProp(propFromParent) {
const [propFromState, setProp] = useState(propFromParent);
useEffect(() => {
setProp(propFromParent);
}, [propFromParent]);
return [propFromState, setProp];
}
export { useDynamicProp };
Which versions of React?
16.12.0
Metadata
Metadata
Assignees
Labels
No labels