admin管理员组文章数量:1289759
I'm rendering a ponent via React Router 4 using render={() => </Component />}
I need to pass state to the given ponent i.e: <Game />
export const Routes: React.SFC<{}> = () => (
<Switch>
<Route path="/" exact={true} ponent={Home} />
<Route path="/play-game" render={() => <Game {...this.state} />} />
<Redirect to="/" />
</Switch>
)
To which TS breaks saying:
The containing arrow function captures the global value of 'this' which implicitly has type 'any'
The final goal is to be able to pass the Routes
to my main app: i.e:
export default class App extends Component<{}, AppState> {
public state = {
// state logic
}
public render(): JSX.Element {
return (
<BrowserRouter>
<div className="App">
<Navigation />
<Routes />
</div>
</BrowserRouter>
)
}
}
How could I apply the correct types to suppress this TypeScript error?
I'm rendering a ponent via React Router 4 using render={() => </Component />}
I need to pass state to the given ponent i.e: <Game />
export const Routes: React.SFC<{}> = () => (
<Switch>
<Route path="/" exact={true} ponent={Home} />
<Route path="/play-game" render={() => <Game {...this.state} />} />
<Redirect to="/" />
</Switch>
)
To which TS breaks saying:
The containing arrow function captures the global value of 'this' which implicitly has type 'any'
The final goal is to be able to pass the Routes
to my main app: i.e:
export default class App extends Component<{}, AppState> {
public state = {
// state logic
}
public render(): JSX.Element {
return (
<BrowserRouter>
<div className="App">
<Navigation />
<Routes />
</div>
</BrowserRouter>
)
}
}
How could I apply the correct types to suppress this TypeScript error?
Share Improve this question asked Jan 14, 2019 at 16:54 Jonca33Jonca33 3,4937 gold badges28 silver badges39 bronze badges 4-
If you are using the arrow function in an
export const
who do you expectthis
to be ? – Titian Cernicova-Dragomir Commented Jan 14, 2019 at 16:56 -
Have you looked into the
bind
function previously? – JO3-W3B-D3V Commented Jan 14, 2019 at 16:57 -
I take
this
would belong to const Routes, which is why is the error? How could I remedy that? I'm still pretty raw. – Jonca33 Commented Jan 14, 2019 at 17:00 -
I suggest getting familiar with
this
in JS - developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Älskar Commented Jan 14, 2019 at 17:02
1 Answer
Reset to default 7Arrow functions do not have lexical contexts, so any invocation of this
inside the body of an arrow will degenerate to its value in the outer scope. This is what TS is plaining about.
For your problem of passing the state around you need to pass this as a prop to the Routes
ponent which will dispatch it to the relevant route.
export default class App extends Component<{}, AppState> {
public state = {
// state logic
}
public render(): JSX.Element {
return (
<BrowserRouter>
<div className="App">
<Navigation />
<Routes state={this.state}/>
</div>
</BrowserRouter>
)
}
}
// you need to pass the correct type to React.SFC<>
// probably something along React.SFC<{ state: State }>
// where State is the type of `state` field in App.
export const Routes: React.SFC<...> = ({ state }) => (
<Switch>
<Route path="/" exact={true} ponent={Home} />
<Route path="/play-game" render={() => <Game {...state} />} />
<Redirect to="/" />
</Switch>
)
本文标签:
版权声明:本文标题:javascript - TypeScript - ReactRouter | Arrow function captures the global value of 'this' which implicitly has 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741396523a2376411.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论