You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native Redux报错:Connect(Books)找不到store求助

Fixing "Could not find 'store' in either the context or props" Error in React Native + Redux

Hey there! Let's sort out that Redux store error you're facing. The root issue here is your Redux Provider is only wrapping the Home component inside StartScreen, but your Books and Electronics screens are rendered directly via react-navigation's stack navigator—they're not part of the Provider's context tree. That's why the Connect(Books) component can't locate the store.

Here's how to fix this step by step:

1. Move the Redux Provider to the Top of Your App

Wrap your entire navigation container with the Provider so every screen in your stack can access the store. Update your App.js like this:

import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import HomeScreen from './src/shopping/Home';
import StartScreen from './src/shopping/StartScreen';
import BooksScreen from './src/shopping/Books';
import Electronics from './src/shopping/Electronics';
// Add these two imports
import { Provider } from 'react-redux';
import store from './src/shopping/store';

const AppNavigator = createStackNavigator(
  {
    Home: HomeScreen,
    Start: StartScreen,
    Elec: Electronics,
    Book: BooksScreen,
  },
  {
    initialRouteName: 'Start',
    defaultNavigationOptions: {
      headerTitle: 'Shopping App',
    },
  }
);

const AppContainer = createAppContainer(AppNavigator);

// Wrap the entire app container with Provider
export default function App() {
  return (
    <Provider store={store}>
      <AppContainer />
    </Provider>
  );
}

2. Remove Redundant Provider from StartScreen

Now that the Provider is at the top, you don't need it in StartScreen anymore. Update StartScreen.js:

import React, { Component } from 'react';
import { View, Text } from 'react-native';
import Home from './Home';
// Remove these imports—they're no longer needed
// import store from './store'
// import { Provider } from 'react-redux';

class StartScreen extends Component {
  render() {
    return (
      <View>
        {/* Just render Home directly */}
        <Home navigation={this.props.navigation} />
      </View>
    );
  }
}

export default StartScreen;

3. Fix Action Type Mismatch in Reducer

You've got a small bug in your reducer too: your dispatch uses type: "ADD_PRODUCTS" but the reducer listens for "ADD_ITEMS". Plus, you need to return a new state array to trigger Redux updates. Update reducer.js:

const items = (state = [], action) => {
  switch (action.type) {
    // Match the action type from your dispatch
    case "ADD_PRODUCTS":
      console.warn(action.payload);
      // Return a new array with the new item added
      return [...state, action.payload];
    // Always include a default case to return the original state
    default:
      return state;
  }
}

export default items;

Why This Works

By placing the Provider at the very top of your app hierarchy, every screen in your react-navigation stack becomes a child of the Provider. This means all connected components (like Books and Electronics) can access the store through context, eliminating the "could not find store" error. The reducer fix ensures that when you dispatch actions, your store actually updates with the new product data.

内容的提问来源于stack exchange,提问作者Sumit Shukla

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 15:02:37