reactjs - 反应上下文没有更新
问题描述
我已经从嵌套道具切换到我的组件到 React 的 Context API。我创建了一个类来为我提供一些所需的方法:
export default class StepDatabase {
private readonly steps: Steps = steps;
private currentStep: number = steps[0].step;
public getStep(): Step {
return this.steps[this.currentStep];
}
public nextStep(): void {
if (this.currentStep === this.steps.length) return;
this.currentStep++;
}
}
然后,创建了一个上下文:
const stepsInstance = new StepsDatabase();
export const StepsContext = createContext<StepsDatabase>(stepsInstance);
当然,然后提供它:
const App = () => (
<div className={styles.App_container}>
<main className={styles.App_grid}>
<StepsContext.Provider value={stepsInstance}>
<Sidebar />
<Content />
</StepsContext.Provider>
</main>
</div>
);
并尝试在我的Sidebar
组件中使用它:
const Sidebar = () => {
const StepContext = React.useContext(StepsContext);
const currentStep = StepContext.getStep();
return (
<section className={`${styles.App_gridItem} ${styles.App_sideItem}`}>
<SidebarHeading>{currentStep.stepName}</SidebarHeading>
<SidebarParagraph>{currentStep.stepDescription}</SidebarParagraph>
<button onClick={() => StepContext.nextStep()}>step</button>
</section>
);
};
但是在单击我的按钮后, SidebarHeading
andSidebarParagraph
根本没有更新。第一步工作得很好。有什么问题?
解决方案
您的代码中没有任何内容会触发上下文重新呈现。如果上下文不重新渲染,它将无法触发所有使用它的组件。您需要更高级别的东西来导致上下文重新渲染,或者您需要将上下文中的函数传递给可能触发重新渲染的消费者。请参阅文档。
这是基于您的代码的示例:
import React, { createContext, useState } from "react";
import "./styles.css";
const StepsContext = createContext();
const Sidebar = () => {
const { step, setNextStep } = React.useContext(StepsContext);
return (
<section>
<div>Heading: {step.stepName}</div>
<div>Paragraph: {step.stepDescription}</div>
<button onClick={() => setNextStep()}>step</button>
</section>
);
};
export default function App() {
const [steps, setSteps] = useState([
{ stepName: "Step 1", stepDescription: "My description 1" },
{ stepName: "Step 2", stepDescription: "My description 2" }
]);
const [currentStep, setCurrentStep] = useState(0);
return (
<div>
<main>
<StepsContext.Provider
value={{
step: steps[currentStep],
setNextStep: function () {
if (currentStep < steps.length - 1) {
setCurrentStep(currentStep + 1);
}
}
}}
>
<Sidebar />
<div>Content</div>
</StepsContext.Provider>
</main>
</div>
);
}
推荐阅读
- java - 在这个多线程 Java 应用程序中,似乎只有一个线程像顺序程序一样运行?
- c# - 需要将包含'/'的日期时间转换为'-'格式
- html - 我试图使用 BeautifulSoup 抓取一个网站
- java - 如何将文本文件内容添加到文本文件包含空格的组合框中?
- ios - 在swift 5中隐藏或禁用uidocumentinteractioncontroller的共享按钮
- android - REDMi Note 5 中未安装 Hello World Android 项目
- kotlin - Kotlin 1.4 中的函数接口
- ios - 从 Android Studio 构建 Flutter 项目时它失败了,但不是从 Xcode(在 Flutter 升级之后)
- android - Glide 4.X:DataFetcher - 尝试从 ZIP 文件加载图片
- codenameone - 将 Youtube iOS 应用中的 URL 分享到我的 Codename One iOS 应用