【发布时间】:2021-07-04 14:35:34
【问题描述】:
试图理解上下文 api,我理解 props 是传下来的。我正在尝试将 Context 文件值的状态更改为另一个数字,例如 50。
创建的上下文文件
import React, { useState, createContext } from "react";
export const PointsContext = createContext();
export const PointsProvider = (props) => {
const [points, setPoints] = useState(0);**<--WANT TO CHANGE THIS**
return <PointsContext.Provider value={points}>{props.children}</PointsContext.Provider>;
};
在 App.js 的 Provider 中包装所有内容
import {PointsProvider } from "./PointsContext";
<PointsProvider>
<ChildComponent>
</PointsProvider>
“ChildComponent”是提供的上下文
import React, { useState, useEffect, useContext } from "react";
import { PointsContext } from "../PointsContext";
const value = useContext(PointsContext);
return(
<Button title="ChangeNumber" onPress={() => Change value to 50 }/>
)
【问题讨论】:
标签: reactjs react-native state react-props use-state