如何跟踪选定的组件?

我正在处理编码挑战,并且在我的组件之间来回导航时遇到问题。这是我的父组件的一个非常简化的版本:


import React, { useState } from 'react';


function App() {

  const [component, setComponent] = useState({

    shoppingBasket: true,

    contactDetails: false,

    paymentOptions: false,

    checkout: false,

    summary: false

  });


  const switchState = input => {

    const { name, value } = input.target;

    setComponent({

      ...component,

      [name]: value

    });

  };


  return (

  <>

    {

      component.shoppingBasket && 

      <ShoppingBasket

        shoppingBasket={component.shoppingBasket}

        switchState={switchState}

      />

    }

    {

      component.contactDetails && 

      <ContactDetails

        contactDetails={component.contactDetails}

        switchState={switchState}

      />

    }

    {

      component.paymentOptions && 

      <PaymentOptions

        paymentOptions={component.paymentOptions}

        switchState={switchState}

      />

    }

    {

      component.checkout && 

      <Checkout

        checkout={component.checkout}

        switchState={switchState}

      />

    }

    {

      component.summary && 

      <Summary

        summary={component.summary}

        switchState={switchState}

      />

    }

  </>

  );

}


export default App;


它应该是电子商务页面的结帐屏幕,它从<ShoppingBasket />组件开始。单击“继续”时,显示下一个组件,单击“返回”时,将返回上一个组件。它们应该按照代码中显示的顺序出现。


我的第一次尝试是仅在前一个组件评估为真时才显示下一个组件,但最后它显示了所有组件,所以这不起作用。注意:我将 switchState 函数和相应的状态作为 prop 传递给子组件。


我想最聪明的方法是只显示当前选择的组件,但我该怎么做呢?我假设使用 ID?


当它只显示选定的组件时,是否仍然需要跟踪评估为 true 的先前组件?


解决方案:

父组件(简化但有效):


import React, { useState } from 'react';


// COMPONENTS

import ShoppingBasket from './components/ShoppingBasket';

import PaymentOptions from './components/PaymentOptions';

import ContactDetails from './components/ContactDetails';

import Checkout from './components/Checkout';

import Summary from './components/Summary';


export default function App() {

  const [component, setComponent] = useState(0);

  const switchComponent = (index) => {

    setComponent(index);

  };



Helenr
浏览 103回答 2
2回答

料青山看我应如是

更新:import React, { useState } from 'react';function App() {&nbsp; &nbsp; const [component, setComponent] = useState(0);&nbsp; &nbsp; const switchComponent = index => {&nbsp; &nbsp; &nbsp; &nbsp; setComponent(index);&nbsp; &nbsp; };&nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; &nbsp; <>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // this act like switch case js function&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (<ShoppingBasket&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //shoppingBasket={component.shoppingBasket} // no need for this&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; componentIndex={component}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switchState={switchComponent}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (<ContactDetails&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // contactDetails={component.contactDetails}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; componentIndex={component}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switchState={switchComponent}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (<PaymentOptions&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // paymentOptions={component.paymentOptions}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; componentIndex={component}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switchState={switchComponent}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (<Checkout&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // checkout={component.checkout}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; componentIndex={component}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switchState={switchComponent}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (<Summary&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // summary={component.summary}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; componentIndex={component}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switchState={switchComponent}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }[component]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; </>&nbsp; &nbsp; );}export default App;

摇曳的蔷薇

ShoppingBasket.jsconst ShoppingBasket = props => {&nbsp; return (&nbsp; &nbsp; // your ShoppingBasket component .....&nbsp; )}** ContactDetails.js **const ContactDetails = props => {&nbsp; return (&nbsp; &nbsp; // your ContactDetails component .....&nbsp; )}.... 其他组件相同应用程序.jsimport React, { useState } from 'react';function App() {const [component, setComponent] = useState(0);const switchComponent = index => {&nbsp; &nbsp; setComponent(index);};return (&nbsp; &nbsp; <>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // this act like switch case js function&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (<ShoppingBasket/>),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (<ContactDetails/>),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (<PaymentOptions/>),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (<Checkout/>),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (<Summary/>)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }[component]&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; // navigation buttons .... always visible&nbsp; &nbsp; &nbsp; <NavigateButtons&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;componentIndex={component}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;switchState={switchComponent}&nbsp;&nbsp; &nbsp; &nbsp; />&nbsp;&nbsp; &nbsp; </>);}导出默认应用程序;----------------------------------***----------------注意:确保下一个和上一个按钮只是一个拆分组件,以便您可以在不同的其他组件中使用它const NavigateButtons = (props) => (&nbsp;<div>&nbsp; &nbsp;<Button&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; disabled={componentIndex == 4}&nbsp;&nbsp; &nbsp; &nbsp; onClick={()=> props.switchState(componentIndex + 1)}>next&nbsp; &nbsp; &nbsp; </Button>&nbsp; &nbsp;<Button&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;disabled={componentIndex == 0}&nbsp; &nbsp; &nbsp; &nbsp;onClick={()=> props.switchState(componentIndex - 1)}>previous&nbsp; &nbsp; &nbsp; &nbsp;</Button></div>)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java