defaultValue change does not re-render input

前端 未结 2 882
栀梦
栀梦 2021-01-17 20:25

I have no idea why this is not working

Demo

I have the following es6 code

const {createFactory, createClass, DOM: { label, input, button }} =         


        
2条回答
  •  南方客
    南方客 (楼主)
    2021-01-17 21:18

    I found what seems to be a pretty good solution to this: Use the key prop to force rendering of an entirely new input.

    In my particular case, I don't need the input to be controlled with its own onChange prop, as the form surrounding it ultimately controls the state within some store which populates the defaultValue. But the store's state might be asynchronously initialized/updated, and in which case the defaultValue should be updated. So here is a condensed version of my particular case:

    import React, { PropTypes } from 'react';
    import { Form } from 'provide-page';
    
    const GatherContact = ({
      classes,
      onSubmit,
      movingContactName,
      movingContactEmail,
      movingContactPhone,
      userName,
      userEmail,
      userPhone
    }) => (
      

    How can we contact you?

    {userName ? undefined : ( ) }
    ); GatherContact.propTypes = { classes: PropTypes.object.isRequired, onSubmit: PropTypes.func.isRequired, movingContactName: PropTypes.string.isRequired, movingContactEmail: PropTypes.string.isRequired, movingContactPhone: PropTypes.string.isRequired, userName: PropTypes.string.isRequired, userEmail: PropTypes.string.isRequired, userPhone: PropTypes.string.isRequired }; export default GatherContact;

提交回复
热议问题