简体   繁体   English

React Native Context 在包裹在内部时呈现空白屏幕<provider></provider>

[英]React Native Context rendering a blank screen when wrapped inside <Provider>

I'm trying to build a simple blog native app using context and have stumbled upon an issue to which I can't find a root to.我正在尝试使用上下文构建一个简单的博客本机应用程序,但偶然发现了一个我找不到根源的问题。

Here's the structure of it:这是它的结构:

/context/createDataContext.js file: /context/createDataContext.js 文件:

import React, { useReducer } from "react"; 
export default (reducer, actions, initialState) => {
const Context = React.createContext();
const Provider = ({ childern }) => {
const [state, dispatch] = useReducer(reducer, initialState);
const boundActions = {};
for (let key in boundActions) {
  boundActions[key] = actions[key](dispatch);
}
return (
  <Context.Provider value={{ state, ...boundActions }}>
    {childern}
  </Context.Provider>
);
};
return { Context, Provider };
};

/context/BlogContext.js: /上下文/BlogContext.js:

import createDataContext from "./createDataContext";

const blogReducer = (state, action) => {
switch (action.type) {
case "add_blogpost":
  return [...state, { title: `Blog Post Number ${state.length + 1}` }];
default:
  return state;
}
};
const addBlogPost = (dispatch) => {
return () => {
dispatch({ type: "add_blogpost" });
};
};
export const { Context, Provider } = createDataContext(
blogReducer,
{ addBlogPost },
[]
);

/screens/IndexScreen.js: /屏幕/IndexScreen.js:

 import React, { useContext } from "react"; import { View, Text, StyleSheet, FlatList, Button } from "react-native"; import { Context } from "../context/BolgContext"; const IndexScreen = () => { const { state, addBlogPost } = useContext(Context); return ( <View> <Button title="Add a blod post" onPress={addBlogPost} /> <FlatList data={state} keyExtractor={(blogPost) => blogPost.title} renderItem={({ item }) => { return <Text>{item.title}</Text>; }} /> </View> ); }; const styles = StyleSheet.create({}); export default IndexScreen;

And finally App.js:最后是 App.js:

 import { NavigationContainer } from "@react-navigation/native"; import { createStackNavigator } from "@react-navigation/stack"; import IndexScreen from "./src/screens/IndexScreen"; import { Provider } from "./src/context/BolgContext"; import React from "react"; const Stack = createStackNavigator(); export default function App() { return ( <NavigationContainer> { <Provider> <Stack.Navigator initialRouteName="Home"> <Stack.Screen name="Home" component={IndexScreen} options={{ title: "My app" }} /> </Stack.Navigator> </Provider> } </NavigationContainer> ); }

Now I did some debugging, even though the code does't come back with any error, but the issue seems to be on my Provider, since if I remove it I can see content on the screen.现在我做了一些调试,即使代码没有返回任何错误,但问题似乎出在我的提供程序上,因为如果我删除它,我可以在屏幕上看到内容。 Does anybody know why this happens.有谁知道为什么会这样。

Thanks a lot!非常感谢!

You need to change the Provider method like below您需要像下面这样更改 Provider 方法

Form形式

const Provider = ({ childern }) => {

To

const Provider = (props) => {

Then you can destructure while passing to the content.provider like below然后你可以在传递给 content.provider 的同时进行解构,如下所示

    <Context.Provider value={{ state, ...boundActions }}>
    {props.childern}
  </Context.Provider>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React:包裹在React.Component中时,应用程序不呈现到屏幕上 - React: App not rendering to screen when wrapped in React.Component Context Provider React Native中的导航功能如何 - How do navigation inside function in Context Provider React Native 如何在反应原生上下文提供程序中调用 function - How to call function inside react native context provider 反应路由器呈现空白屏幕 - React Router Rendering a Blank Screen 当组件树周围没有提供者时,React Context defaultValue 返回 undefined - React Context defaultValue returns undefined when no provider is wrapped around the component tree 当我用 Provider 包装我的应用程序组件时,为什么会出现此错误? Redux 和 React Native - Why am I getting this error when I wrapped my App component with Provider? Redux and React Native 找不到 react-redux 上下文值; 请确保组件被包裹在一个<provider>尽管组件被包装在提供者中</provider> - Could not find react-redux context value; please ensure the component is wrapped in a <Provider> although component is wrapped in a provider 找不到 react-redux 上下文值; 请确保组件被包裹在一个<provider>即使已经包含在提供程序中</provider> - Could not find react-redux context value; please ensure the component is wrapped in a <Provider> even if already wrapped in a provider React Context Provider 所有子项重新渲染 - React Context Provider all children re rendering 提供者标签使屏幕 go 在反应中空白 - Provider tag makes screen go blank in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM