簡體   English   中英

如何從 React 中不同組件的狀態更新進度條

[英]how to update progress bar from states in different components in React

我有一個正在構建的應用程序,它的頁腳中有一個進度條。 HTML 和 CSS 是完美的,我只是不知道如何添加功能。 這是酒吧的 html:

     <section className="3xl:scale-125">
       <ul class="step-wizard-list">
           <li class="step-wizard-item current-item">
               <span class="progress-count">1</span>
               <span class="progress-label">Get started</span>
           </li> 
           <li class="step-wizard-item">
               <span class="progress-count">2</span>
               <span class="progress-label">Connect to Metamask</span>
           </li>
           <li class="step-wizard-item">
               <span class="progress-count">3</span>
               <span class="progress-label">Connect to Polygon</span>
           </li>
           <li class="step-wizard-item">
               <span class="progress-count">4</span>
               <span class="progress-label">Claim NFT</span>
           </li>
       </ul>
   </section>
   </div>

current-item 是進度條如何知道它應該如何更新 css。 我需要做的是基於 3 個不同組件中的狀態(或者甚至是 onClicks,如果這更容易)以某種方式將 current-item 添加到每個<li>中。

我試圖弄清楚如何使用 react useContext 來做到這一點,但問題是狀態不是來自 1 個單個組件。 任何建議將不勝感激!

如果您需要更多信息,我可以分享一個回購。

創建一個容器並使用 React.useSatate。

const [step,setActiveStep]=useState(0)
<PogressContainer>
  {stepsComponents[step]}
  <ProgressBar activeStep={step}/>
</PorgressContainer>

stepsComponents=[
<Step1 activeStep={step}/>,
<Step2 activeStep={step}/>,
<Step3 activeStep={step}/>]

這只是一個想法。

要在多個組件中使用上下文,您需要在更高級別上提供它。

您可以包含更新 function 來更新上下文,然后消費者可以使用它,如文檔中所述: https://reactjs.org/docs/context.html#updating-context-from-a-nested-component

type ProgressBar = {
  currentItem: number;
  updateCurrentItem: (item: number) => void;
};

export const ProgressBarContext: Context<ProgressBar> = createContext({});

export default function App() {
  const [progressBar, setProgressBar] = useState({
    currentItem: 0,
    updateCurrentItem: (i: number) =>
      setProgressBar((prev) => ({ ...prev, currentItem: i })),
  });

  return (
    <ProgressBarContext.Provider value={progressBar}>
      <MakeProgress />
      <ProgressBar />
    </ProgressBarContext.Provider>
  );
}

您可以從任何組件更新當前項目,如下所示:

export default function MakeProgress() {
  return (
    <ProgressBarContext.Consumer>
      {({ updateCurrentItem }) => (
        <div>
          <button onClick={() => updateCurrentItem(1)}>Complete 1</button>
          <button onClick={() => updateCurrentItem(2)}>Complete 2</button>
          <button onClick={() => updateCurrentItem(3)}>Complete 3</button>
          <button onClick={() => updateCurrentItem(4)}>Complete 4</button>
        </div>
      )}
    </ProgressBarContext.Consumer>
  );
}

讀取當前項,基本相同。 您可以使用className動態更改 class 列表。

export default function ProgressBar() {
  return (
    <ProgressBarContext.Consumer>
      {({ currentItem }) => (
        <ul>
          <li className={currentItem >= 1 ? 'current-item' : ''}>1</li>
          <li className={currentItem >= 2 ? 'current-item' : ''}>2</li>
          <li className={currentItem >= 3 ? 'current-item' : ''}>3</li>
          <li className={currentItem >= 4 ? 'current-item' : ''}>4</li>
        </ul>
      )}
    </ProgressBarContext.Consumer>
  );
}

Stackblitz: https://stackblitz.com/edit/react-ts-4pttoq?file=App.tsx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM