Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not find “store” in either the context or props of “Connect(SomeComponent)” #1569

Closed
soberservicesguy opened this issue Apr 18, 2020 · 2 comments

Comments

@soberservicesguy
Copy link

soberservicesguy commented Apr 18, 2020

What is the current behavior?

  1. I created a react native app using below command
    expo init new_project

  2. entered new_project directory and isntalled redux and redux saga using following commands
    npm install redux
    npm install --save redux-saga
    npm install react-redux
    npm install

  3. ran the app using
    npm start OR
    yarn start

The content of my files are below:

App.js

import React from 'react';
import { View, Text,StyleSheet,} from "react-native";
import { connect } from "redux"; 
import {Provider} from "react-redux"; 
import {mapStateToProps, mapDispatchToProps} from "./store/store_configuration";
import {store} from "./saga/saga_configuration";

class SomeComponent extends Component {
    constructor(props) {
        super(props);
    }
    render() {
        return (
          <View style={styles.container}>
            <Text style={{color:'#000000'}}>
              Open up App.js to started working on your app!
            </Text>
          </View>          
        );
    }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    color:'#000000',
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(SomeComponent);

function App() {
  return (
    <Provider store={store}>
      <SomeComponent />
    </Provider>
  );
}

store_configuration.js

import { combineReducers } from 'redux'
// IMPORT all reducers
import reducerA from "./reducer_a";
import reducerB from "./reducer_b";

export const rootReducer = combineReducers({
  rA:reducerA, 
  rB:reducerB
});

export const mapStateToProps = state => {
  return {
    a: state.rA.a,
    b: state.rB.b
  };
};

export const mapDispatchToProps = dispach => {
  return {
    updateA: b => dispach({ type: "UPDATE_A", b: b }),
    updateB: a => dispach({ type: "UPDATE_B", a: a })
  };
};

reducer_a.js

const initialState = {
  age: 20
};

const reducerA = (state = initialState, action) => {
  const newState = { ...state };

  switch (action.type) {
    case "AGE_UP_ASYNC1":
      newState.age += action.value;
      break;

    case "AGE_DOWN1":
      newState.age -= action.value;
      break;
  }
  return newState;
};

export default reducerA;

saga_configuration.js

import {createStore, applyMiddleware} from "redux";
import createSagaMiddleware from "redux-saga";
// IMPORT root reducer
import {rootReducer} from "../store/store_configuration";
// IMPORT all saga watchers
import {watchAgeUp} from "./saga_a";

// Create rootSaga
function* rootSaga(){
  yield all([
    fork(watchAgeUp),
  ]);
};

const sagaMiddleWare = createSagaMiddleware();
export const store = createStore(rootReducer, applyMiddleware(sagaMiddleWare));
sagaMiddleWare.run(rootSaga); // passed rootSaga which includes all saga watchers

saga_a.js

import { delay } from "redux-saga";
import { takeLatest, takeEvery, put } from "redux-saga/effects";

function* ageUpAsync() {
  yield delay(4000);
  yield put({ type: "AGE_UP_ASYNC", value: 1 });
}

export function* watchAgeUp() {
  yield takeLatest("AGE_UP", ageUpAsync);
}

What is the expected behavior?
It should run properly, I tried to create new app and tried to reproduce this error again it happened again. Please point out mistake if you see one. The error I am getting is

Could not find “store” in either the context or props of “Connect(SomeComponent)”

@soberservicesguy soberservicesguy changed the title Getting Error TypeError:(0, _redux.connect) is not a function Could not find “store” in either the context or props of “Connect(SomeComponent)” Apr 18, 2020
@markerikson
Copy link
Contributor

markerikson commented Apr 18, 2020

As I just commented over in Reactiflux: you're putting that connected component outside a Provider. All connected components must be inside a Provider.

@soberservicesguy
Copy link
Author

Thanks for your support at Reactiflux, I have used your instructions and done the project at https://github.com/soberservicesguy/react_native_with_proper_redux

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants