简体   繁体   中英

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:

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:

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:

 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:

 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

Form

const Provider = ({ childern }) => {

To

const Provider = (props) => {

Then you can destructure while passing to the content.provider like below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM