简体   繁体   English

如何在不使用 Switch Case 语句的情况下显示下一步和上一步

[英]How to show Step next and previous without using Switch Case statement

I have array list ie我有数组列表,即

   const data = [
      {
        title: 'Title1',
      },
      {
        title: 'Title2',
      },
      {
        title: 'Title3',
      },
      {
        title: 'Title4',
      },
    ];

that needs to be render inside Modal along with the Next and Previous button I can achieve using Switch Case , seems like this not a feasible solution.需要在 Modal 中渲染以及我可以使用Switch Case实现的NextPrevious按钮,这似乎不是一个可行的解决方案。

any idea how to render title and when the user click on Next button it should change the content also.任何想法如何呈现标题,当用户单击下一步按钮时,它也应该更改内容。 here is my switch case solution.这是我的开关盒解决方案。

    const [showStep, setShowStep] = useState(1);
    const renderModal = () => {
    switch (showStep) {
          case 1:
            return (
              <Modal
                open={show}
                onClose={() => setShow(false)}
                onSubmit={() => setShowStep(2)}
                title={'User Type'}
                number={1}
                total={3}
              />
            );
          case 2:
            return (
              <Modal
                open={show}
                onClose={() => setShow(false)}
                onSubmit={() => setShowStep(3)}
                title={'UserClass'}
                number={2}
                total={3}
                previous={
                  <ButtonComponent
                    onClick={() => setShowStep(1)}
                    name={'Previous'}
                    color={'primary'}
                    variant={'outlined'}
                  />
                }
              />
            );
          case 3:
            return (
              <Modal
                open={show}
                onClose={() => setShow(false)}
                onSubmit={() => setShowStep(4)}
                title={'Class'}
                number={3}
                total={3}
                previous={
                  <ButtonComponent
                    onClick={() => setShowStep(2)}
                    name={'Previous'}
                    color={'primary'}
                    variant={'outlined'}
                  />
                }
              />
            );
          default:
            return null;
        }

}

How about this?这个怎么样? Makes your code more dynamic.使您的代码更加动态。

const renderModal = () => {
    return (
              <Modal
                open={show}
                onClose={() => setShow(false)}
                onSubmit={() => setShowStep(showStep + 1)}
                title=data[showStep].title
                number={showStep}
                total=data.lenght
                previous={
                  <ButtonComponent
                    onClick={() => setShowStep(showStep - 1)}
                    name={'Previous'}
                    color={'primary'}
                    variant={'outlined'}
                  />
                }
              />
            );
}

You can try state management like this, with this your modal is a complete different component and you can handle what content to show in the modal through the parent component.您可以尝试像这样管理 state ,这样您的模态是一个完全不同的组件,您可以通过父组件处理要在模态中显示的内容。

The useEffect is completely optionally, I just added to reset the modal to initial value. useEffect 完全是可选的,我只是添加了将模式重置为初始值。

const data = [
  {
    title: 'Title1',
  },
  {
    title: 'Title2',
  },
  {
    title: 'Title3',
  },
  {
    title: 'Title4',
  },
];

const ModalWithButtons = ({ detail, onClickNextButton, onClickBackButton, showModal, setShowModal }) => {
  return (
    <Modal
    open={showModal}
    onClose={() => setShowModal(false)}
    onSubmit={onClickNextButton}
    title={detail.title}
    previous={
      <ButtonComponent
        onClick={onClickBackButton}
        name={'Previous'}
        color={'primary'}
        variant={'outlined'}
      />
    }
  />
  );
};

export default function ParentComponent(props) {
  const [showModal, setShowModal] = React.useState(false);
  const [showDetailNo, setShowDetailNo] = React.useState(0);

  useEffect(() => {
    // resetting the modal number to 0
    if(!showModal)
      setShowDetailNo(0);
    
  }, [showModal]);

  const handleButtonClick = () => {
    setShowModal(prevShowModal => (!prevShowModal));
  }

  const handleOnClickBackButton = () => {
    /* we can disable the previous button in the modal when showDetailNo is 0 */
    if(showDetailNo === 0)
      return;

    setShowDetailNo(prevShowDetailNo => (prevShowDetailNo - 1));
  }

  const handleOnClickNextButton = () => {
    /* we can disable the next button in the modal when showDetailNo is data.length-1 */
    if(showDetailNo === data.length-1)
      return;

    setShowDetailNo(prevShowDetailNo => (prevShowDetailNo + 1));
  }

  return (
    <div className="App">
      <button onClick={handleButtonClick}>Show Modal</button>
      {showModal && <ModalWithButtons 
        showModal={showModal}
        setShowModal={setShowModal}
        detail={data[showDetailNo]}
        onClickBackButton={handleOnClickBackButton}
        onClickNextButton={handleOnClickNextButton}
      />}
    </div>
  );
}

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

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