Skip to content

Props, hooks, and re-rendering #17712

@elliotcm

Description

@elliotcm

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions