我理解的 href 和 link 之间的区别。
但是我想知道使用link和withrouter和history.push的区别?如果我已经访问过该页面,history.push 会检索缓存中的页面吗?
使用链接:
<Link className="btn btn-primary" onClick={logout} to="/">
Log out
</Link>
Run Code Online (Sandbox Code Playgroud)
使用历史:
constructor(props) {
super(props);
this.handleLogout = this.handleLogout.bind(this);
};
handleLogout(e) {
const { history } = this.props;
logout()
history.push("/");
}
<button type="button" onClick={this.handleLogout}>Log out</button>
Run Code Online (Sandbox Code Playgroud) 使用 Flow 打字的 react 路由器的 history.push 可以使用什么类型?
type State = {
...
history: string,
}
Run Code Online (Sandbox Code Playgroud)
登录成功后,我使用react路由器路由到路由
history.push("/user");
Run Code Online (Sandbox Code Playgroud)
但是出现以下错误,我在flow中没有找到类型
Cannot call `history.push` because property `push` is missing in `String` [1]. (References: [1])
Run Code Online (Sandbox Code Playgroud) 我有一个组件,使用数组中的信息创建按钮,当用户单击按钮我想将此数组中的信息传递给函数,但我得到'[对象对象]',我如何通过数组并访问函数中的元素?
功能:
handleClick (e) {
const { value } = e.currentTarget
console.log(value)
};
Run Code Online (Sandbox Code Playgroud)
组件渲染:
render () {
return (
<div>
{this.state.enrollments.map(item => (
<div key={item.id}>
<button value={item} onClick={this.handleClick}>
{item.enrollment}
</button>
</div>
))}
</div>
)
}
Run Code Online (Sandbox Code Playgroud)
数组传递给map:
0: {id: 1, evaluation_flag: false, enrollment: "2019.1", user_id: 2}
1: {id: 2, evaluation_flag: false, enrollment: "2019.2", user_id: 2}
Run Code Online (Sandbox Code Playgroud)
在handleClick函数中,我想访问值,键入:value.id或value.user_id.