在底部,在 NavLink 標記中,onClick 不起作用,我只被重定向到另一個頁面但它不處理 submitHandler 函數如果我刪除 NavLink,則 submitHandler 進程,但每當我添加 React Router 標簽時,什么都不會觸發我刪除了大部分代碼,因為我的問題并不重要class XXX extends React.Component { constructor() { super(); this.state = { username: "", password: "", users: [], errors: [] }; } submitHandler = event => { event.preventDefault(); let check = true; if (this.state.username == "") { this.showValidationErr("username", "Username cannot be empty !"); check = false; } if (this.state.password == "") { this.showValidationErr("password", "Password cannot be empty !"); check = false; } if (this.state.users.indexOf(this.state.username) != -1) { this.showValidationErr("username", "Username already exists !"); check = false; } if (check) { axios .post("https://localhost:44314/api/Users", this.state) .then(response => { console.log(response); }) .catch(error => { console.log(error); }); } }; render() { return ( <div className="App"> <form onSubmit={this.submitHandler}> <button type="Submit"> <NavLink to="/newsfeed" onClick={() => this.submitHandler}> Create Account </NavLink> </button> </form> </div> ); }}
React Router - OnClick 不在 Navlink 中處理
慕沐林林
2022-05-26 17:35:36