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

314
Visualizações
React setState + Where does 'prevState' come from?

I just started learning React and JavaScript.
While going through the tutorial, I got to this example code of a component, which creates a toggle button.
This is part of the code:

class Toggle extends React.Component {
    constructor(props) {
        super(props);
        this.state = {isToggleOn: true};
        this.handleClick = this.handleClick.bind(this);
    }

    handleClick() {
        this.setState(prevState => ({    // prevState?
            isToggleOn: !prevState.isToggleOn
        }));
    }

2 things that are bugging me here:

  1. Where did the prevState argument come from?
    I don't see anything like var prevState = this.state; before calling it, and still, it works.
  2. The syntax of the arrow function: why the parentheses after the arrow?
    Why doesn't the usual arg => { statement; } syntax work here?

Sorry for the newbie questions...

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

  1. prevState is provided by React along with props, both of which are optional.

    • Update 04/13/19: React has changed the setState function documentation by renaming prevState to updater. The callback function still takes two arguments; the state and props at the time the change is being applied.
  2. The parenthesis allow multiple lines where if you didn't use the parenthesis you'd be forced to used a return. You could use a single line but you don't need the curly braces.

    • Update: I forgot to mention a specific case where it is required to have parenthesis. If you're returning an object without a return statement you must wrap it in parenthesis. Thank you @joedotnot for catching that. So () => {foo: true} will throw an error because it looks like a function and foo: true is an invalid line. To fix this it must look like () => ({ foo: true })
over 4 years ago · Santiago Trujillo Relatório

0

Im use this. (Example)

const [modal, setModal] = useState(false);
const [dataAction, setDataAction] = useState({name: '', description: ''});

const _handleChangeName = (data) => {
    if(data.name)
        setDataAction( prevState  => ({ ...prevState,   name : data.name }));
    if(data.description)
        setDataAction( prevState  => ({ ...prevState,   description : data.description }));
  };
over 4 years ago · Santiago Trujillo 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