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

514
Visualizações
How would I rewrite this without an arrow function (regular function)

I'm not so good with arrow functions and would like some practice changing from arrow function to regular function. This is within React.

function render() {
  const todoItems = this.state.todos.map(
    item => <TodoItem
      key={item.id}
      item={item}
      handleChange={this.handleChange}
    />
  );
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Arrow args

There are a few general forms of arrow functions.

  • arg1 => is strictly for one argument
  • (arg1, arg2) => more than one argument, enclose in parentheses.
  • () => no arguments also requires parentheses

Arrow braces

On the other side of the arrow, there are another 2 forms:

  • => value returns value (or the result of an expression)
  • => {someStatements;} does not implicitly return anything.

Converting to function()

When converting to functions, follow this table:

  • arg1 => becomes function(arg1)
  • (arg1, arg2) => becomes function(arg1, arg2)
  • => value becomes function(args) { return value }
  • => { someStatements;} becomes function(args) { someStatements;}

this: different for arrow functions

Lastly, a subtle but important difference. Arrow functions use this from their wrappers. Functions declared with function keyword always have their own this.

Putting it all together

So in this particular case, given todos.map(item => <Component />), the function is item => <Component /> and working backwards from the table above we can see that this is equivalent to the function function(item) { return <Component />; }

The full line looks like this:

const outerThis = this; //save with a different name so we can access within function
const todoItems = this.state.todos.map(
    function(item) { 
        return <TodoItem key={item.id} item={item} handleChange={outerThis.handleChange}/>
    }
)

See also Arrow Function Expression on MDN, where you can read about a few other arrow function caveats (like no arguments, new, prototype or yield) and this operator.

about 4 years ago · Juan Pablo Isaza Relatório

0

If you want transform an arrow function to regular function, follow this code:

    const todoItems = this.state.todos.map(handleMap)
    
    function handleMap(item) {
      return <TodoItem key={item.id} item={item} handleChange={this.handleChange}/>
    }

But, I recommend to use function components and React hooks. Read this artice: https://reactjs.org/docs/hooks-intro.html

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