简体   繁体   English

为什么onclick称为react material-ui

[英]why onclick called react material-ui

I write react component with dialog and list. 我用对话框和列表编写react组件。 When component recieve "open" prop and try to build list in render function, it's call onClick={ this.handleToggle(item.id) } function of ListItem, so, that causes an infinite loop "render -> onClick -> setState -> render -> ... ". 当组件收到“打开”的道具并尝试在渲染函数中构建列表时,它调用ListItem的onClick={ this.handleToggle(item.id) }函数,因此,这将导致无限循环“渲染-> onClick-> setState- >渲染-> ...“。 Why? 为什么? And how can I get around this? 我该如何解决呢?

handleToggle(value) {
   if (this.state.isLoading) {
       return;
   }

    const {checked} = this.state;
    const currentIndex = checked.indexOf(value);        
    const newSelected = checked.slice();

    if (currentIndex === -1) {
        newSelected.push(value);
    } else {
        newSelected.splice(currentIndex, 1);
    }
    this.setState({checked: newSelected});
}

render() {  
    if (!this.props.show) {
        return null;
    }
    if (this.state.isLoading) {
        return(<p>Loading...</p>);
    }

    const {classes, show, caption} = this.props;
    const {checked, projects} = this.state;

    return (
        <Dialog
            open={show}
            onClose={this.onCancel}
            aria-labelledby="form-dialog-title">
                <DialogTitle id="form-dialog-title">{caption}</DialogTitle>
                <DialogContent>
                    <List>
                        {projects.map(item => 
                            <ListItem
                                key={item.id}
                                role={undefined}
                                dense
                                button
                                onClick={ this.handleToggle(item.id) }
                                className={classes.listItem}
                            >
                                <Checkbox 
                                    tabIndex={-1} 
                                    disableRipple
                                    checked={checked.indexOf(item.id) !== -1}
                                />
                                <ListItemText primary={item.name}/>
                            </ListItem>
                            ) }
                    </List>
                </DialogContent>
                <DialogActions>
                    <Button onClick={this.onSuccess} color="primary">OK</Button>
                    <Button onClick={this.onCancel} color="secondary">Cancel</Button>
                </DialogActions>
        </Dialog>
    );
}

onClick={ this.handleToggle(item.id) } will cause the handleToggle function to be evaluated everytime render occurs and since you call a setState in handleToggle, it will go into an infinite loop. onClick={ this.handleToggle(item.id) }会导致每次渲染发生时都要对handleToggle函数进行评估,并且由于您在handleToggle中调用了setState,它将陷入无限循环。 onClick expects a function and you should write it like onClick需要一个功能,您应该像这样编写它

onClick={() => this.handleToggle(item.id) }

this will cause handleToggle to be called only on a click event 这将导致仅在click事件上调用handleToggle

You have to write it like 你必须像这样写

onClick={ () => this.handleToggle(item.id) }

because writing onClick={ this.handleToggle(item.id) } instantly calls the method instead of giving it as a callback to the onClick event 因为编写onClick={ this.handleToggle(item.id) }立即调用该方法,而不是将其作为对onClick事件的回调

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM