1

I have a component that i need to pass props into while being rendered using the Route component. The problem is that typescript disallows lambda functions in JSX and i just can't figure out how to rewrite this into an allowed function:

<Route
   key={index}
   exact={true}
   path={item.urlAddress}
   render={() => <DocumentView data={data} />}
/>

I tried this, but it is still being evaluated as a lambda, don't know why:

render={function() { <DocumentView /> }}

Anyone knows how to rewrite it?

Edit: I know this is a linter issure, and i'm looking for the proper way to write this according to ts-lint standards. I know i can add a rule exception, but i want to know how to do this the "proper" way, not adding exceptions every time i encounter a linting problem

1 Answers1

6

You can make a change in the tslint rules to allow for arrow functions in render. However, since you asked an alternative way to write the above. It would be written like

renderDocument = (props) => {
   return <DocumentView data={data} {...props}/>
}
render() {
    return (
        <Route
           key={index}
           exact={true}
           path={item.urlAddress}
           render={this.renderDocument}
        />
    )
}
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
  • You could read this answer as well, it does explain a lot of things in details https://stackoverflow.com/questions/45053622/how-to-avoid-binding-inside-render-method/45053753#45053753 – Shubham Khatri Oct 04 '18 at 06:52