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

62
Visualizações
How to initialize the value of state on the basis of props and then change the state on dropdown selection in React?

I have a component with a dropdown having two options. 'Existing Members' and 'Add New Member'.

If the value of a props.existingMembers.length > 0. No default value should be selected in dropdown. However if the props.existingMembers.length === 0. 'Add New Member' should be selected as the default value in dropdown.

In ComponentDidMount I am making an API call with the help of which props.existingMembers is fetched. Since props.existingMembers always have length 0 in first render(Before API call). I am unable to achieve above requirement.

class AssignOwnershipDialog extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            dropDownValue: '',
        };
    }
    componentDidMount() {
        fetchExistingMembersList();
    }
    static getDerivedStateFromProps(props, state) {
        if (props.existingMembers.length === 0) {
            return {
                dropDownValue: 'Add New'
            };
        }
        if (props.existingMembers.length === 0) {
            return {
                dropDownValue: ''
            };
        }
    }

    handleDropDownSelection = (dropDownValue) => {
        this.setState({
            dropDownValue
        });
    }

    render() {
   const {dropDownValue}=this.state;
        return ( <React.Fragment>
            <Select value = {dropDownValue}
            onChange = {(e) => this.handleDropDownSelection(e.target.value)}>
                <MenuItem value = "Add New" key = {0}> Add New </MenuItem> 
                <MenuItem value = "Existing Member" key = {1}> Existing Member </MenuItem> 
                </Select> 
                </React.Fragment>
            );
        }
    }

The above approach seems to be not working as the value of dropdown is not changing

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

0

You can add an item with empty value so if the state is empty nothing will be shown in dropdown

there is a minor mistake

static getDerivedStateFromProps(props, state) {
    if (props.existingMembers.length === 0) {
        return {
            dropDownValue: 'Add New'
        };
    }
    // mistake
    if (props.existingMembers.length > 0) {
        return {
            dropDownValue: ''
        };
    }
}
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