- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在尝试在 React Native 中使用上下文 Hook ,但它似乎不起作用,它返回 undefined。但是,当我使用 <Context.Consumer>
时它确实工作正常,你知道 React Native 是否支持 useContext 吗?
最佳答案
在 react native 中绝对支持 useContext。
使用 React.createContext() 创建上下文。
export const AppStateContext = React.createContext();
const AppStateProvider = props => {
const contextValue={...yourContext}
return (
<AppStateContext.Provider value={contextValue}>
{props.children}
</AppStateContext.Provider>
);
};
<AppStateProvider>
<App />
</AppStateProvider>
import {AppStateContext} from './AppStateProvider';
function YourComponent(props) {
const {context} = useContext(AppStateContext)
...
return (
<View>
...
</View>
);
}
关于reactjs - React Native 中的 useContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58060118/
最近进入一个项目,在react中的useContext看到了以前没见过的东西。 项目中的全局状态使用它发送到上下文的钩子(Hook),然后当稍后在全局状态上调用该钩子(Hook)时,全局状态是可访问的
我使用了一个结构 React Hooks .它基于全局 Context包含 reducer 的组合(如在 Redux 中)。 另外,我广泛使用custom hooks分离逻辑。 我有一个包含异步 AP
在 React 18.1.0 和 React-Dom 18.1.0 中使用 @mui/material 时,我遇到了情感和 react 之间的一系列兼容性问题。这很奇怪,因为我实际上安装了最新版本的
我试图弄清楚如何正确使用 React Context。我对尝试从函数组件外部访问 Context 的问题感到困惑。我收到错误: Line 9:18: React Hook "useContext" i
努力学习如何使用 react 钩子(Hook) .我用 创建上下文 我想将状态和功能一起转移到其他组件,但我需要有关如何执行此操作的帮助。我也想问这样做是否有意义。 事实上,我可以使用 来更新状态设置
我有两个组件 Parent 和 Child,我想将上下文从 Parent 导出到 Child,但这会导致循环依赖。 例如,考虑 Parent.js 是 import {Child} from './C
我对 React 非常着迷,但我遇到了一个关于 useContext 的大问题。真正了解它的内部工作原理是相当令人困惑的。就像它神奇地工作一样。 所以我的主模块中有这样的: export const
更新配置文件时,profile.name 会通过导航链接 (Navigation.js) 中的 useContext() 显示。但是,一旦页面刷新或注销期间,profile.name 就会被清除,pr
我试图以最简单的方式设置 useContext,但它一直返回 undefined。 我已经检查了好几次,但仍然无法破译我遗漏的内容。 上下文.js: import React from 'react'
更新配置文件时,profile.name 会通过导航链接 (Navigation.js) 中的 useContext() 显示。但是,一旦页面刷新或注销期间,profile.name 就会被清除,pr
我试图以最简单的方式设置 useContext,但它一直返回 undefined。 我已经检查了好几次,但仍然无法破译我遗漏的内容。 上下文.js: import React from 'react'
我遇到了一个非常令人沮丧的问题,我不知道发生了什么。我有一个简单的上下文,如下所示: import React, { useState, createContext } from "react"; e
我正在尝试掌握 React 中新的 useContext 函数。在无状态功能组件中效果很好。例如: import React from 'react'; import LocaleContext fr
如果我在 Comp1.js const Comp1 = () => { const globalTheme = new createContext() return (
我无法弄清楚为什么我的 useContext 没有在这个函数中被调用: import { useContext } from "react"; import { MyContext } from ".
我在子组件中有一个按钮 AddMore.js它在列表中添加了更多项目,但它不会重新呈现呈现该列表的其他子组件 Pig.js .我认为我的设置会起作用,因为我使用 NextJS 框架以类似的方式设置了上
你好 我正在尝试进行菜单切换,其中我有一个以 false 作为初始值的变量,使用 react createContext 和 useContext Hook ,我将初始状态设置为 true // us
我有问题,我已经设置了 useContext与 useState提供程序中的值,但是当我导入它们并尝试更改值时,它会在本地更改而不是全局更改! 登录.js import { UserContext }
我正在尝试使用 React Context 来管理我的项目的状态,但这次我似乎无法让它适用于我的 CRA 代码库,而我在其他项目上进行了成功的尝试。 useContext 返回未定义,因此我无法访问上
我是 useContext 的新手钩子(Hook)并试图找出我做错了什么......没有顶级导入,这就是我的代码的样子: 所以我的项目的根是这样的: export default function A
我是一名优秀的程序员,十分优秀!