javascript - Define variable and return component from an arrow function -
i'd define variable inside .map()
iteration, returning component.
but having variable inside map doesn't work (gives me error). possible @ all, , if how do this?
below simplified example of i'm trying do:
render() { return( <div> {array.map( (element, index) => ( let disturbingvariable = 100 + index <mycomponent disturbingvariable={disturbingvariable} /> ))} </div> ) }
when arrow function has more 1 statement can no longer use implicit return syntax.
add block braces , return
statement:
array.map((element, index) => { let disturbingvariable = 100 + index return <mycomponent disturbingvariable={disturbingvariable} /> })
alternatively, forgo variable declaration , perform addition in-place, maintaining implicit return:
array.map((element, index) => <mycomponent disturbingvariable={100 + index} />)
Comments
Post a Comment