简体   繁体   English

有条件地显示3个组件中的1个

[英]Show one out of 3 components conditionally React

I have built a toggle component that shows one out of two components based on whether the Purchase is true or not. 我已经构建了一个切换组件,该组件根据Purchase是否为true显示两个组件中的一个。 Now I want to select from 3 components and I'm struggling with refactoring this so it's clean and works. 现在,我想从3个组件中进行选择,而我正努力对其进行重构,以使其干净并可以正常工作。 What is the best way to do this if I'm adding Component3 both to the toggle selectors and the component import? 如果将Component3都添加到切换选择器和组件导入中,最好的方法是什么?

import Component1 from "./component1";
import Component2 from "./component2";

class App extends Component {
    constructor(props) {
        super(props);

        // this.toggle = this.toggle.bind(this);
        this.state = {
            popoverOpen: false,
            isPurchase: true,
        };

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

    switchState(flag) {
        this.setState({ isPurchase: flag });
    }
    render() {
        return (
              <div className={styles.cardBody}>
                                        <div className={styles.row}>
                                            <div className={styles.col12}>
                                                <div
                                                    className={`${
                                                        styles.positionRelative
                                                    } ${styles.formGroup}`}>
                                                    <div
                                                        style={{
                                                            fontWeight:
                                                                "bolder",
                                                            color: "#7192a6",
                                                        }}>
                                                        <span
                                                            className={
                                                                this.state
                                                                    .isPurchase
                                                                    ? `${
                                                                          styles.switchState
                                                                      }`
                                                                    : `${
                                                                          styles.switchState
                                                                      } ${
                                                                          styles.unselected
                                                                      }`
                                                            }
                                                            onClick={() => {
                                                                this.switchState(
                                                                    true,
                                                                );
                                                            }}>
                                                            Component1{" "}
                                                        </span>
                                                        /
                                                        <span
                                                            className={
                                                                this.state
                                                                    .isPurchase
                                                                    ? `${
                                                                          styles.switchState
                                                                      } ${
                                                                          styles.unselected
                                                                      }`
                                                                    : `${
                                                                          styles.switchState
                                                                      }`
                                                            }
                                                            onClick={() => {
                                                                this.switchState(
                                                                    false,
                                                                );
                                                            }}>
                                                            {" "}
                                                            Component2
                                                        </span>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div className={styles.row}>
                                            <div className={styles.col12}>
                                                <Component1
                                                    isPurchase={
                                                        this.state.isPurchase
                                                    }
                                                />
                                                <Component2
                                                    isPurchase={
                                                        this.state.isPurchase
                                                    }
                                                />
                                            </div>
                                        </div>
                                    </div>
        );
    }
}

And in the component itself I'm checking this in the state 在组件本身中,我正在检查状态

class Component1 extends Component {
    constructor(props) {
        super(props);
        this.state = {
            isPurshase: props.isPurchase,
            popoverOpen: false,

        };
    }

And inside the return display/hide this way 并在返回显示/隐藏内部

<div style={{ display: this.props.isPurchase ? "" : "none" }}>

You can do something like this to be more clean. 您可以做这样的事情以使其更干净。

class App extends Component {
    state = {
        condition: 'condition1',
    };

    handleSwitch = () => {
        const {condition} = this.state;
        let newCondition = '';
        switch (condition) {
            case 'condition1':
                newCondition = 'condition2';
                break;
            case 'condition2':
                newCondition = 'condition3';
                break;
            case 'condition3':
                newCondition = 'condition1';
                break;
            default:
                newCondition = 'condition1';
        }

        this.setState({
            condition: newCondition,
        });
    };

    renderSwitch = () => {
        <button onClick={() => this.handleSwitch()}> CHANGE </button>;
    };

    renderComponent = () => {
        const {condition} = this.state;
        switch (condition) {
            case 'condition1':
                return (<ComponentOne/>);
            case 'condition2':
                return (<ComponentTwo/>);
            case 'condition3':
                return (
                    <div>
                        <ComponentOne/>
                        <ComponentOne/>
                    </div>
                );
            default:
                return null;
        }
    }

    render() {
        return (
            <div>
                {this.renderSwitch()}
                {this.renderComponent()}
            </div>
        );
    }
}

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

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