Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.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
Javascript 在执行该函数之前请等待。下面是我制作的一个codesandbox,它详细阐述了我的问题。请注意:setInterval中的thispoiner没有指向组件的this。第一行应该是这样的:varthat=this;this.interval=setInter_Javascript_Reactjs - Fatal编程技术网

Javascript 在执行该函数之前请等待。下面是我制作的一个codesandbox,它详细阐述了我的问题。请注意:setInterval中的thispoiner没有指向组件的this。第一行应该是这样的:varthat=this;this.interval=setInter

Javascript 在执行该函数之前请等待。下面是我制作的一个codesandbox,它详细阐述了我的问题。请注意:setInterval中的thispoiner没有指向组件的this。第一行应该是这样的:varthat=this;this.interval=setInter,javascript,reactjs,Javascript,Reactjs,在执行该函数之前请等待。下面是我制作的一个codesandbox,它详细阐述了我的问题。请注意:setInterval中的thispoiner没有指向组件的this。第一行应该是这样的:varthat=this;this.interval=setInterval(()=>that.getData(),10000)我正在研究WebSocket,不完全确定从何处开始修改。 cDM() { //axios call, update state with fetched data } onCh


在执行该函数之前请等待。下面是我制作的一个codesandbox,它详细阐述了我的问题。请注意:
setInterval
中的
this
poiner没有指向组件的
this
。第一行应该是这样的:
varthat=this;this.interval=setInterval(()=>that.getData(),10000)我正在研究
WebSocket
,不完全确定从何处开始修改。
cDM() {
    //axios call, update state with fetched data
}

onChange1 () {
    // uses data from cDM to render the options in a dropdown.
    // another axios call based on the selection, fetches data to render more options in subsequent dropdown
}

onChange2 () {
    //dropdown 2 use data from onChange1 axios call. After the selection from the dropdown is made, it makes an api call that then renders data to a table. 
    //Some of this data updates every 5 seconds, so I need to re-poll this service to get updated information from the server.
}
componentDidMount() {
this.interval = setInterval(() => this.getData(), 10000);
this.getData();
}

componentWillUnMount() {
clearInterval(this.interval);
}

getData = () => {
//data requests
}
onChange () {    
    // Change Logic
    ....

    // Interval logic
    clearInterval(this.state.interval); //Clear interval everytime.

    this.state.interval = setInterval(() => this.getData(), 10000);
    this.getData();
}

componentWillUnMount() {
    clearInterval(this.state.interval);
}
interface Props<T> {
  intervalMilliseconds: number
  render: React.ComponentType<ChildProps<T>>
}

interface State<T> {
  // In this example I assume your select option will be some kind of string id, but you may wish to change this.
  option: string | undefined
  isFetching: boolean
  data: T
}

interface ChildProps<T> extends State<T> {
  setOption(option: string | undefined): void
}

class WithPolledData<T> extends React.Component<Props<T>, State<T>> {
  interval: number

  componentDidMount() {
    this.setupSubscription()
  }

  componentDidUnmount() {
    this.teardownSubscription()
  }

  setupSubscription() {
    this.interval = setInterval(() => this.fetch(), this.props.intervalMilliseconds)
  }

  teardownSubscription() {
    clearInterval(this.interval)
  }

  componentDidUpdate(previousProps: Props, previousState: State) {
    if (previousProps.intervalMilliseconds !== this.props.intervalMilliseconds) {
      this.teardownSubscription()      
    }
    if (previousState.option !== this.state.option && this.state.option !== undefined) {
      this.fetch()
    }
  }

  fetch() {
    if (this.props.option === undefined) {
      // There is nothing to fetch
      return
    }

    // TODO: Fetch the data from the server here with this.props.option as the id
  }

  setOption = (option: string | undefined) => {
    this.setState({ option })
  }

  render() {
    return React.createElement(
      this.props.render,
      {
        ...this.state,
        setOption: this.setOption
      }
    )
  }
}
<WithPolledData intervalMilliseconds={5000} render={YourComponent} />