javascript - 如何在反应类的渲染方法中使用来自回调函数的数据
问题描述
我是 javascript 新手并做出反应,对于我的实例,我需要使用扩展类的函数xmlHttpRequest
内部的数据render
react.component
我已经使用 awindow.localstorage
来保存结果并在其他地方使用它。但这看起来不像是正确的方法。
我需要的:
class MyClass extends Component {
MyFunction() { //the function that includes a callback function
setInterval(function() { //the callback function
//get some data
}, 1000)
}
render() {
return (
<div>
</div>
)
}
}
现在如何在渲染中使用这些数据?
解决方案
获取您在回调中获得的任何响应,您之前用于保存到localstorage
,并将其设置为 state。
const response = res
this.setState({response})
然后在您的渲染中引用相同的内容
render(){
return(
<div>{this.state.response}</div>
)
}
推荐阅读
- android - 更新到 WorkManager 1.0.0-alpha09 后编译错误
- python - 如何合并两个缺少数据的数据框
- python - 当其他 2 列的元组是唯一的时,Pandas 添加新列并用列表中的项目填充它
- android - 如何处理警告:未经检查地调用“ObservableField(T)”作为原始类型“android.databinding.ObservableField”的成员
- node.js - npm 错误!cb() 从未调用过!尝试运行 npm install 命令时出错
- gatsby - 了解 Gatsby 2 页面依赖树
- javascript - 传递给异步库中各个任务的回调的作用
- php - PHP中的字符串文字是否只能以兼容的ASCII超集编码进行编码,例如UTF-8或ISO-8859-1?
- java - Java while-loop 不会在给定命令上停止(初学者程序员)
- android - 将android应用程序连接到服务器并使其运行python程序并从应用程序获取输入?