Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.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/Javascript:componentDidMount()和render_Javascript_Reactjs_Graphql_React Apollo - Fatal编程技术网

ReactJS/Javascript:componentDidMount()和render

ReactJS/Javascript:componentDidMount()和render,javascript,reactjs,graphql,react-apollo,Javascript,Reactjs,Graphql,React Apollo,渲染函数中的控制台日志显示数据。componentDidMount的console.logthis.props未定义???。这怎么可能?我不明白异步在React中是如何工作的 class Graph extends React.Component { constructor(props) { super(props); this.state = { startTime:this.props.startTime, endTime:t

渲染函数中的控制台日志显示数据。componentDidMount的console.logthis.props未定义???。这怎么可能?我不明白异步在React中是如何工作的

class Graph extends React.Component {
  constructor(props) {
    super(props);

    this.state = {  startTime:this.props.startTime, 
                    endTime:this.props.endTime,
                    nodes:this.props.data
                  }
    //console.log('graph data:', this.props.data)
  }

  componentDidMount() {
   // console.log('nodes:', this.props.data)
    this.force = d3.forceSimulation(this.state.nodes)
      .force("charge",
        d3.forceManyBody()
          .strength(this.props.forceStrength)
      )
      .force("x", d3.forceX(this.props.width / 2))
      .force("y", d3.forceY(this.props.height / 2));

    this.force.on('tick', () => this.setState({data: this.props.data}));
    console.log('setState:', this.props.data)
  }

  componentWillUnmount() {
    this.force.stop();
  }

  render() {
   // console.log('graph datas:', this.props.data)
    return (

      <svg width={this.props.width} height={this.props.height}>
      {console.log('map data:', this.props.data)}
      {Object.keys(this.props.data).map((node, index) =>(
     <circle r={node.r} cx={node.x} cy={node.y} fill="red" key={index}/>
      ))}
      </svg>
    );
  }//render
}//Component

export default graphql(getObjectsQuery, 
  { options: (ownProps) => { 
    console.log(ownProps.startTime); 
    return ({ second: { startTime: ownProps.startTime,
                            endTime: ownProps.endTime
     } }) 
  } } )(Graph);
安装组件时,将同步调用componentDidMount挂钩。在重新渲染时不调用它

如各国所述:

componentDidMount在将组件装入树后立即调用。需要DOM节点的初始化应该在这里进行。如果需要从远程端点加载数据,这是实例化网络请求的好地方

此时未定义this.props.data


而每次重新渲染时都会触发渲染挂钩。这个问题没有说明如何使用该组件,但是我希望我的数据值在语法方面有困难。你能提供一个我上面的D3代码的例子吗。我对React和D3非常陌生。我无法让它工作,因为它只是更大事物的一部分。这就是问题所在。graphql怎么样。。正在使用的出口?语法到底有什么问题?这基本上就是你的情况。请注意,数据最初在渲染中也是未定义的。可能是。我仍然不确定你在寻找什么样的例子。你手边已经有工作实例了,不是吗?你发布的代码在我看来还行,你所描述的行为也是意料之中的。我更新了,请注意,如果您需要使用一些逻辑来关注这个.props.data,您需要将它同时放入componentDidMount和componentDidUpdate中。我不确定到底是什么问题。在我点击提交按钮后,程序是否符合要求且提交按钮向下移动,但屏幕为空白?这与您最初的问题有很大不同,当然需要反映您当前案例的内容,最好是一个可行的演示,因为它需要调试。我不使用GraphQL。你可以查一下