Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
42
rated 0 times [  43] [ 1]  / answers: 1 / hits: 16135  / 7 Years ago, thu, june 29, 2017, 12:00:00

I have a component with a login form.



What I need to do is to get it to reload the page / route after the user clicks the submit button.



The reason for this is that after the submit button is clicked some changes are needed on the nav component which is not this one.



You can see that I use sessionStorage.setItem('something', 'somevalue'); which then the nav component reads and makes some changes.



At the moment to get to see the changes I need to reload the browser.



Here is the code:



class Login extends Component {
constructor(props) {
super(props);
this.state = {
username: '',
password: '',
};

this.onUsernameChange = this.onUsernameChange.bind(this);
this.onPasswordChange = this.onPasswordChange.bind(this);
}

onUsernameChange(event) {
this.setState({ username: event.target.value });
}

onPasswordChange(event) {
this.setState({ password: event.target.value });
}

handleSubmit() {
event.preventDefault();
sessionStorage.setItem('something', 'somevalue');
// reload the page here or re-router to current page/router

}

render() {

return (
<div>
<form className=form-signin onSubmit={this.handleSubmit}>
<input type=text value={this.username} onChange={this.onUsernameChange} />
<input type=password value={this.password} onChange={this.onPasswordChange} />
<input type=submit value=Submit />
</form>
</div>
);
}
}

export default Login;


How can I fix this problem?


More From » reactjs

 Answers
38

A good idea would be to use onEnter callbacks in your routes or subscribe to changes on sessionStore (is that a redux store?) to check for updates, and allow that information to propagate down through the app.



To answer your question about navigation, however, you can use either:



location.reload() - location is a standard JavaScript (browser) object



or inject React Router's router using the provided withRouter component enhancer.



import { withRouter } from 'react-router'

...

export default withRouter(Login);


router will be available in props - you can use it like



const { router } = this.props
router.push('/some/route')


this will trigger a view change without reloading the window (which is why I was suggesting making sure that you can update your component's knowledge about the session without reloading the page)



edit - by the way event is undefined in handleSubmit


[#57264] Tuesday, June 27, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lamontm

Total Points: 482
Total Questions: 99
Total Answers: 91

Location: Burkina Faso
Member since Thu, Dec 15, 2022
1 Year ago
;