В коде, который вы показываете, вы должны установить новое состояние в обратных вызовах then
или catch
.Вы можете иметь некоторые дополнительные свойства в состоянии вашего компонента для достижения этого.
...
this.state = {
complete: false,
referrer: '',
email: '',
amount: '',
code: null,
subscription_days: null,
error: null,
};
...
И затем, вы должны установить его так:
...
.then(response => {
console.log('Received Stripe token:', response.token);
axios.post('http://10.250.57.37:8000/subscriptions/codes/pay/',
{
token: response.token,
amount: this.state.amount,
email: this.state.email,
referrer: this.state.referrer, // rn name or empty string, filip
},
{
'Content-Type': 'application/json', // header
}
)
// Use the appropiate property in your response to set the values.
// Note that I'm using destructuring assignment
.then(({ code, subscription_days })=> {
this.setState({
code,
subscription_days
});
});
})
.catch(error => {
this.setState({
error: `Your error message.`//Maybe error.message?
});
});
...
Наконец, я бы порекомендовал вытащитьвыведите свой сетевой код вызова из компонента в отдельный модуль и просто верните ответ.Это сделает ваш код компонента более читабельным.