Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 将新的this.state发送到服务器_Reactjs - Fatal编程技术网

Reactjs 将新的this.state发送到服务器

Reactjs 将新的this.state发送到服务器,reactjs,Reactjs,环境。 -后端框架烧瓶 -前端框架 我试图向服务器发送一个用户在下拉菜单中选择的年份值。同时,我将状态更改为相同的值。但从我所看到的情况来看,我只能在新值呈现后将其发送到服务器。我该怎么做。 这是我的代码: 从“React”导入React 从“/年”导入年数 要求('es6-PROMICE')。polyfill(); require('isomorphic-fetch'); 类HeaderYear扩展了React.Component{ 建造师(道具){ 超级(道具) this.state={d

环境。
-后端框架烧瓶
-前端框架

我试图向服务器发送一个用户在下拉菜单中选择的年份值。同时,我将状态更改为相同的值。但从我所看到的情况来看,我只能在新值呈现后将其发送到服务器。我该怎么做。
这是我的代码:

从“React”导入React
从“/年”导入年数
要求('es6-PROMICE')。polyfill();
require('isomorphic-fetch');
类HeaderYear扩展了React.Component{
建造师(道具){
超级(道具)
this.state={date:this.props.year}
this.dropdownFunction=this.dropdownFunction.bind(this)
this.sendStateToServer=this.sendStateToServer.bind(this)
this.changeYear=this.changeYear.bind(this)
}
下拉函数(){
document.getElementById(“myDropdownYear”).classList.toggle(“show”);
}
sendStateToServer(){
//设置全局变量
让statenow=this.props.year
var有效载荷={
年份:statenow
}
log('payload是'+JSON.stringify(statenow));
获取(“/年”,
{
/*选项仅在POST方法中需要*/
方法:“POST”,
标题:{
“接受”:“应用程序/json”,
“内容类型”:“应用程序/json”
},
正文:JSON.stringify(有效负载),
}
)。然后(响应=>{
如果(response.status>=400){
抛出新错误(“来自服务器的错误响应”);
}
response.json().then(数据=>{
控制台日志(数据);;
})
})
}
变更年份(年){
this.setState({date:year})
console.log(this.state.date);
this.sendStateToServer()文件
}
渲染(){
//如果用户在下拉菜单外单击,请关闭下拉菜单
window.onclick=函数(事件){
如果(!event.target.matches('.dropbtn')){
var dropdowns=document.getElementsByClassName(“下拉内容”);
var i;
对于(i=0;i
{this.props.years.map(year=>{
返回
})}

)
}
}

导出默认HeaderYear
您必须在setState()函数的回调函数中传递sendStateToServer()函数。
this.setState({date:year},()=>{
sendStateToServer();

})
我没有帮忙。每次更改时我都会发送上一个状态。我知道了。我将新状态添加到回调函数参数中,如下所示:this.setState({date:year},()=>{sendStateToServer(this.state.date);})。并更新了sendStateToServer,现在可以运行了。谢谢