Lo que estoy tratando de hacer es limpiar un estado de mis accesorios de objetos en mi componente de clase en el cambio de pantalla
aquí es donde estoy representando los campos:
class InputBody extends Component { constructor(props) { super(props); this.state = { fields: JSON.parse(this.props.route).message, }; } render() { return ( <Fragment> {Object.keys(JSON.parse(this.props.route).message).length > 0 ? ( <FieldArraysForm all={JSON.parse(this.props.route).message} native={this.props} /> ) : ( <ActivityIndicator size="large" color="#eb6b09" /> )} </Fragment> ); } }Luego estoy manejando mis campos y tipos de entrada aquí:
class RenderField extends Component { render() { return ( <Fragment> <Texto>{this.props.label}</Texto> <TextInput onChangeText={this.props.input.onChange} {...this.props.input} keyboardType={this.props.type} /> </Fragment> ); } }Y finalmente obtengo los valores:
class FieldArraysForm extends Component { render() { const {handleSubmit} = this.props.native; // event listener const getFields = async (values) => { return sleep(500).then(() => { // implementar prop.reset() as soon as I leave the screen, but how can I make it ? console.log(JSON.stringify(values)) }) } return ( <Form> {this.props.all.map((item) => ( <Field key={item._id} name={`customInput.${item._id}`} component={RenderField} label={item.field} type={item.typeFieldAltText} /> ))} <View> <TouchableOpacity onPress={handleSubmit(getFields)}> <Text>Save Form</Text> </TouchableOpacity> </View> </Form> ); } } Alguien sabe cómo puedo limpiar mi estado en class component .
Obs: estoy usando: react-navigation v6 .
Realice una función de reinicio en su componente InputBody y pase esa función como accesorio a su componente FieldArrayForm.
class InputBody extends Component { constructor(props) { super(props); this.state = { fields: JSON.parse(this.props.route).message, }; } reset () { this.setState({}); } render() { return ( <Fragment> {Object.keys(JSON.parse(this.props.route).message).length > 0 ? ( <FieldArraysForm all={JSON.parse(this.props.route).message} resetForm={reset} native={this.props} /> ) : ( <ActivityIndicator size="large" color="#eb6b09" /> )} </Fragment> ); } }Luego llame a los accesorios de reinicio en su componente FieldArraysForm
const getFields = async (values) => { return sleep(500).then(() => { // implementar prop.reset() as soon as I leave the screen, but how can I make it ? console.log(JSON.stringify(values)) this.props.reset() }) }