Redux form defaultValue

后端 未结 3 532
别那么骄傲
别那么骄傲 2021-01-01 11:43

How to set defaultValue to input component?


<         


        
3条回答
  •  旧时难觅i
    2021-01-01 12:15

    this is my implementation using a HoC

    import { Component } from 'react'
    import {
      change,
    } from 'redux-form'
    
    class ReduxFormInputContainer extends Component{
      componentDidMount(){
        const {
          initialValue,
          meta,
        } = this.props
        if(initialValue === undefined || meta.initial !== undefined || meta.dirty) return
        const {
          meta: { form, dispatch },
          input: { name },
        } = this.props
        dispatch(change(form, name, initialValue))
      }
      render(){
        const {
          initialValue,
          component: Compo,
          ...fieldProps
        } = this.props
        return 
      }
    }
    
    function reduxFormInputContainer(component){
      return function(props){
        return 
      }
    }
    
    export default reduxFormInputContainer
    

    and then for exemple:

    import reduxFormInputContainer from 'app/lib/reduxFormInputContainer'
    
    InputNumericWidget = reduxFormInputContainer(InputNumericWidget)
    
    class InputNumeric extends Component{
      render(){
        const props = this.props
        return (
          
        )
      }
    }
    

提交回复
热议问题