Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
How to clean the state on screen change ?(using conditional rendering)

What I'm trying to do is clean a state of my objects props in my class component on screen change

here is where I'm rendering the fields:

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>
    );
  }
}

Then I'm handling my fields and input types in here:

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>
    );
  }
}

And finally I'm getting the values:

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>
    );
  }
}

Someone knows how can I clean my state in class component ?

Obs: I'm using: react-navigation v6.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Make a reset function in your InputBody component and pass that function as a prop to your FieldArrayForm compoent.

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>
    );
  }
}

Then call that reset props in your FieldArraysForm component

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()
      })
    }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!