Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

513
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda