React Admin - Get current value in a form

前端 未结 2 2147
甜味超标
甜味超标 2021-02-20 08:02

I am having big troubles getting the \"updated\" value of a record in an edit form. I always get the initial record values, even though I have an input linked to the right recor

2条回答
  •  清歌不尽
    2021-02-20 08:24

    It really depends on what you want to do with those values. If you want to hide/show/modify inputs based on the value of another input, the FormDataConsumer is the preferred method:

    For example:

    import { FormDataConsumer } from 'react-admin';
    
    const OrderEdit = (props) => (
        
            
                
                
                    {({ formData, ...rest }) =>
                         
                    }
                
            
        
    ); 
    

    You can find more examples in the Input documentation. Take a look at the Linking Two Inputs and Hiding Inputs Based On Other Inputs.

    However, if you want to use the form values in methods of your MyEditForm component, you should use the reduxForm selectors. This is safer as it will work even if we change the key where the reduxForm state is in our store.

    import { connect } from 'react-redux';
    import { getFormValues } from 'redux-form';
    
    const mapStateToProps = state => ({
        recordLiveValues: getFormValues('record-form')(state)
    });
    
    export default connect(mapStateToProps)(MyForm);
    

提交回复
热议问题