简体   繁体   English

如何通过更改状态使按钮在 React.js 中充当单选按钮?

[英]How to make buttons act as radio buttons in React.js by changing states?

I am fairly new to React and wanted to try my hand at making a pathfinding visualizer.我对 React 还很陌生,想尝试制作一个寻路可视化工具。 I want to have buttons that can be clicked which would make that particular pathfinding algorithm active while all else are inactive but I am unable to achieve this.我希望有可以单击的按钮,这将使特定的寻路算法处于活动状态,而其他所有算法都处于非活动状态,但我无法实现这一点。

I have an Options component which renders the buttons:我有一个呈现按钮的选项组件:

export class Options extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            dijkstra: false,
            aStar: false,
            bestSearch: false,
            bfs: false,
            dfs: false,
        };

        this.handleClick = this.handleClick.bind(this);
    }

    handleClick(name) {
        this.setState({
            dijkstra: false,
            aStar: false,
            bestSearch: false,
            bfs: false,
            dfs: false,
        });

        this.setState({ [name]: true });
    }

    render() {
        return (
            <div className='toolbar'>
                <div className='algorithms'>
                    <Button
                        name="Dijkstra's Algorithm"
                        type='dijkstra'
                        isActive={this.state.dijkstra}
                        onClick={this.handleClick}
                    />
                    <Button
                        name='A* Algorithm'
                        type='aStar'
                        isActive={this.state.aStar}
                        onClick={this.handleClick}
                    />
                    <Button
                        name='Best First Search'
                        type='bestSearch'
                        isActive={this.state.bestSearch}
                        onClick={this.handleClick}
                    />
                    <Button
                        name='BFS'
                        type='bfs'
                        isActive={this.state.bfs}
                        onClick={this.handleClick}
                    />
                    <Button
                        name='DFS'
                        type='dfs'
                        isActive={this.state.dfs}
                        onClick={this.handleClick}
                    />
                </div>
                <div className='info'></div>
                <div className='legend'></div>
            </div>
        );
    }
}

the state is used to keep track of which algorithm is active while handleClick changes the state based on the key of the button. state 用于跟踪哪个算法处于活动状态,而 handleClick 根据按钮的键更改 state。 The following is the code for the Button component:以下是 Button 组件的代码:

export class Button extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            isActive: false,
        };
    }

    componentWillReceiveProps() {
        if (this.props.isActive) this.setState({ isActive: true });
    }

    render() {
        let active = "";
        if (this.state.isActive) {
            active = "active";
        }
        return (
            <button
                className={`button ${active}`}
                onClick={this.props.onClick(this.props.type)}
            >
                {this.props.name}
            </button>
        );
    }
}

I receive an error "Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops."我收到错误消息“超出最大更新深度。当组件在 componentWillUpdate 或 componentDidUpdate 中重复调用 setState 时,可能会发生这种情况。React 限制嵌套更新的数量以防止无限循环。”

Any help with how I can rectify this or a better way to implement this?有什么帮助我可以纠正这个或更好的方法来实现这个吗?

When you do this:当你这样做时:

<button
    className={`button ${active}`}
    onClick={this.props.onClick(this.props.type)}
>

it is going to set onClick to whatever this.props.onClick(this.props.type) returns.它将onClick设置为this.props.onClick(this.props.type)返回的任何内容。 So every time it renders, it is going to call this.props.onClick , which is going to call setState , which is going to cause a rerender, which will call it again, etc.所以每次渲染时,它都会调用this.props.onClick ,这会调用setState ,这会导致重新渲染,它会再次调用它,等等。

You probably want to do:你可能想做:

onClick={() => this.props.onClick(this.props.type)}

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

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