Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

93
Visualizações
Laggy TextField Updates in React

I'm relatively new to react and am having a little trouble understanding passing props/states from a child to parent component. I've managed to implement something that works but it is extremely laggy.

Overview: I am trying to create a form with multiple Material UI TextFields, with a submission button at the end to submit the form.

My approach: I am using state to update individual textfields on their inputs, and then dynamically updating another state in the parent FormSection file. Once the user clicks on the 'Submit' Button (not implemented yet), it will then take all the states from the parent class.

Existing Classes:

  1. CompanyField.js
  2. JobTitle.js
  3. FormSection.js (Main File)

Implementation Appearance

CompanyField Class (will be same for Job Title, etc):

const Root = styled('div')(({ theme }) => ({
}));

class CompanyField extends React.Component {

  state = { company: '' }

  handleOnChange = (event) => {
    event.preventDefault();
    this.setState({ company: event.target.value.toLowerCase() });

    this.props.onCompanyChange(this.state.company);
  }

  render() {
    return (
      <Root>
        <Box
          noValidate
          autoComplete="off"
        >
            <TextField
              id = "outlined-basic" 
              label = "Company" 
              variant = "outlined"
              fullWidth

              value = {this.state.company}
              onChange = { this.handleOnChange }
            />
        </Box>
      </Root>
    );
  }
}

export default CompanyField;

Index Class

class FormSection extends React.Component {

  state = { company: '', jobTitle: '' }

  onCompanyUpdate = (value) => {
    this.setState({company: value})
    // console.log('Company:', this.state.company);

  }

  render() {
    return (
          <FormContainer>
            <FormContentHeaderWrapper>
              <FormContentHeader>
                  Company & Job Information
              </FormContentHeader>
            </FormContentHeaderWrapper>

             <FormWrapperFull>
               <CompanyField onCompanyChange={ this.onCompanyUpdate } />
               <JobTitleField onJobTitleChange={ this.onJobTitleUpdate } />
             </FormWrapperFull>
           </FormContainer>
    )
}

Could someone explain whether I am doing this the correct way? Else, would there be a better way to resolve this state passing method?

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When you update the parent component the whole tree re-renders on every keystroke. In your case your component very small it should not be a big impact. There are three approaches in my mind.

first of all, you have to use react developer tools for investigating further re-renders and finding the real problem.

first: You might use form validation libraries. For example; "react hook forms"

second: You might use React's "React.memo" function to memorize components.

third: You might use refs for input value management. You add values to ref and when you need them you iterate that ref object. You don't update the state. If there is no state update there will be no rerender.

for example: In parent component:

const values = useRef({description: "", jobtitle: ""});

const onChange(name, value) {
  values.current[name] = value;
}

In child component: (it must be an "uncontrolled component")

const handleCompanyChange = (evt) => {
  const value = evt.target.value;
  const name = "company";
  props.onChange(name, value);
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda