ReactJS/JavaScript:访问查询结果
在控制台中执行查询后,我可以看到:ReactJS/JavaScript:访问查询结果,javascript,reactjs,Javascript,Reactjs,在控制台中执行查询后,我可以看到:{data:{action:[…]}。如何将该数据分配给React组件中的变量 尝试此操作但不起作用: class MyGraph extends React.Component { constructor(props) { super(props); this.state = { nodes: this.data 为state.node设置一些错误或空的初始值。然后在componentDidMoun
{data:{action:[…]}
。如何将该数据分配给React组件中的变量
尝试此操作但不起作用:
class MyGraph extends React.Component {
constructor(props) {
super(props);
this.state = {
nodes: this.data
为
state.node
设置一些错误或空的初始值。然后在componentDidMount
中获取数据-成功后,使用实际数据更新state.node
。该数据也可以传递给子组件。例如:
class MyGraph extends React.Component {
constructor(props) {
super(props);
this.state = {
nodes: null,
}
}
componentDidMount() {
// w/e your fetch function is. I'm assuming your method uses promises.
fetchDataSomehow().then((response) => {
this.setState({nodes: response.data}) // or whatever property has the nodes
})
}
render() {
const nodes = this.state.nodes;
return (
<LoadingIcon isVisible={!nodes}>
{(nodes || []).map((node) => (
<MyCircle key={node.someUniqueId} node={node} /> // access `node` inside the `<MyCircle />` component using `this.props.node`
))}
</LoadingIcon>
)
}
}
类MyGraph扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
节点:null,
}
}
componentDidMount(){
//您的获取函数是。我假设您的方法使用承诺。
fetchDataSomehow()。然后((响应)=>{
this.setState({nodes:response.data})//或具有节点的任何属性
})
}
render(){
const nodes=this.state.nodes;
返回(
{(节点| |[])。映射((节点)=>(
//使用'this.props.node'访问``组件内的'node'`
))}
)
}
}
您需要处理在数据仍在加载时发生/呈现的情况(例如,组成的
组件)。您将如何获取数据?这就是我试图弄明白的。我可以在控制台中看到数据,但不知道如何访问它。您所说的“在控制台中访问查询”是什么意思?查询是什么?为什么不能将相同的代码直接放入React组件中?共享整个组件如果可以在console中看到它,请尝试查找console.log()声明。另外,Chrome浏览器有一个很好的React扩展,对这类事情很有帮助。谢谢,我会看看我能用它做些什么,并让你知道它是如何运行的。