Warning: Use the 'defaultValue' or 'value' props on <select> instead of setting 'selected' on

前端 未结 5 995
陌清茗
陌清茗 2020-12-29 18:11

SCENARIO A user has a dropdown and he selects an option. I want to display that dropdown and make that option a default value which was selected by that use

5条回答
  •  离开以前
    2020-12-29 18:36

    Thank you all for this thread! My colleague and I just discovered that the default_value property is a Constant, not a Variable.

    In other React forms I've built, the default value for a Select was preset in an associated Context. So the first time the Select was rendered, default_value was set to the correct value.

    But in my latest React form (a small modal), I'm passing the values for the form as props and then using a useEffect to populate the associated Context. So the FIRST time the Select is rendered, default_value is set to null. Then when the Context is populated and the Select is supposed to be re-rendered, default_value cannot be changed and thus the initial default value is not set.

    The solution was ultimately simple: Use the value property instead. But figuring out why default_value didn't work like it did with my other forms took some time.

    I'm posting this to help others in the community.

提交回复
热议问题