I have a Signup page developed with ReactJS and I want after the click of submit button, it redirects me to login page, after that it display a react-notification which message is response.data.message
.
I used react-notifications-ponent
:
import ReactNotification from "react-notifications-ponent";
export default class Signup extends Component {
constructor(props) {
super(props);
this.addNotification = this.addNotification.bind(this);
this.notificationDOMRef = React.createRef();
}
addNotification(message) {
this.notificationDOMRef.current.addNotification( {
title: "Awesomeness",
message:{message},
type: "success",
insert: "top",
container: "top-right",
animationIn: ["animated", "fadeIn"],
animationOut: ["animated", "fadeOut"],
dismiss: { duration: 2000 },
dismissable: { click: true }
});
}
handleSubmit = event => {
event.preventDefault();
const users = {
name:this.state.name,
email:this.state.email,
}
console.log(users)
const { history } = this.props
axios({
method: 'post',
url: 'http://172.16.234.24:8000/api/register',
data: users,
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
'Accept': 'application/json',
}
})
.then(function (response) {
try{
console.log(response);
console.log('yes');
history.push('/login');
this.addNotification(response.data.message);
}
catch{
console.log('no');
}
})
.catch(function (response) {
console.log(response);
});
}
<Button type="submit" onClick={this.handleSubmit}>Register </Button>
<ReactNotification ref={this.notificationDOMRef} />
When I run it, it redirects me to the login page and the notification not working and I get no
in the console of catch
.
How can I fix that or have you a suggestion to use another system for notifications ?
I have a Signup page developed with ReactJS and I want after the click of submit button, it redirects me to login page, after that it display a react-notification which message is response.data.message
.
I used react-notifications-ponent
:
import ReactNotification from "react-notifications-ponent";
export default class Signup extends Component {
constructor(props) {
super(props);
this.addNotification = this.addNotification.bind(this);
this.notificationDOMRef = React.createRef();
}
addNotification(message) {
this.notificationDOMRef.current.addNotification( {
title: "Awesomeness",
message:{message},
type: "success",
insert: "top",
container: "top-right",
animationIn: ["animated", "fadeIn"],
animationOut: ["animated", "fadeOut"],
dismiss: { duration: 2000 },
dismissable: { click: true }
});
}
handleSubmit = event => {
event.preventDefault();
const users = {
name:this.state.name,
email:this.state.email,
}
console.log(users)
const { history } = this.props
axios({
method: 'post',
url: 'http://172.16.234.24:8000/api/register',
data: users,
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
'Accept': 'application/json',
}
})
.then(function (response) {
try{
console.log(response);
console.log('yes');
history.push('/login');
this.addNotification(response.data.message);
}
catch{
console.log('no');
}
})
.catch(function (response) {
console.log(response);
});
}
<Button type="submit" onClick={this.handleSubmit}>Register </Button>
<ReactNotification ref={this.notificationDOMRef} />
When I run it, it redirects me to the login page and the notification not working and I get no
in the console of catch
.
How can I fix that or have you a suggestion to use another system for notifications ?
Share Improve this question edited Feb 25, 2019 at 0:16 Lazar Nikolic 4,4041 gold badge26 silver badges46 bronze badges asked Jan 18, 2019 at 10:47 Ichrak MansourIchrak Mansour 1,95212 gold badges38 silver badges64 bronze badges 6-
Pass an error into the catch and output the error. It will probably give you a better understanding of what is going wrong.
try { ... } catch(error) { console.log(error); }
– JLeggatt Commented Jan 18, 2019 at 10:50 -
@JLeggatt the error is
TypeError: Cannot read property 'addNotification' of undefined
– Ichrak Mansour Commented Jan 18, 2019 at 10:53 -
Ah, so your
this
reference has lost reference. This is because the anonymous function doesn't know whataddNotification
refers to. Try adding a.bind
to the inline function that then calls. – JLeggatt Commented Jan 18, 2019 at 11:04 -
I have already
this.addNotification = this.addNotification.bind(this);
in my constructor – Ichrak Mansour Commented Jan 18, 2019 at 11:07 -
The problem is that you're using the
this
keyword inside the anonymous function thethen
calls. If you console logthis
above thethis.addNotification
you'll see it doesn't have access to thethis.addNotification
– JLeggatt Commented Jan 18, 2019 at 11:10
1 Answer
Reset to default 3You don't need to bind this, you should use arrow functions
.then(response => {
try{
console.log(response);
console.log('yes');
history.push('/login');
this.addNotification(response.data.message);
} catch(error) {
console.log('no');
}
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745432856a4627472.html
评论列表(0条)