Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.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 在无限循环中调用API_Javascript_Reactjs - Fatal编程技术网

Javascript 在无限循环中调用API

Javascript 在无限循环中调用API,javascript,reactjs,Javascript,Reactjs,我有一个需要调用API的模态组件,我是这样做的,isOpen是一个打开和关闭模态的标志,但是通过这种方法,我得到了无限的调用。只要在请求/渲染时使用一个标志,不要一直请求 componentWillReceiveProps(nextProps) { if(nextProps.isOpen){ const { selectedItemId} = this.props; //ajax } } 组件将接收道具

我有一个需要调用API的模态组件,我是这样做的,isOpen是一个打开和关闭模态的标志,但是通过这种方法,我得到了无限的调用。

只要在请求/渲染时使用一个标志,不要一直请求

componentWillReceiveProps(nextProps) {

        if(nextProps.isOpen){
            const { selectedItemId} = this.props;
            //ajax 
        }
    }

组件将接收道具
将在道具发生更改时调用。它可以来自reacts内部状态或您自己的应用程序状态。在
componentDidMount
中编写您的
ajax
,在
isOpen
为真之前不要呈现您的
modal
。我在modal的contstuctor中执行console.log,当我加载包含它的页面时会触发它。“不渲染”是什么意思?
组件将接收props
将被调用
n
多次。您必须在父组件的render方法中有条件地呈现您的模态,如
{this.state.isOpen?():null}
这很奇怪,但我认为Panther所说的更有意义。这取决于您希望该对象如何工作以及需要它如何工作。例如,如果您需要在模式打开时发出请求(假设您单击模式中的某个内容,然后需要发出请求)。您的问题在用例中不是很清楚,所以我只是为您当前的实现编写了它。
componentWillReceiveProps(nextProps) {
    if(nextProps.isOpen && !this.requesting){
        const { selectedItemId } = this.props;
        this.requesting = true;
        //ajax 
        ajax.then( () => {
            this.requesting = false;
        })

    }
}