10

Update 2 - Add minimal 'working' example showing the issue

I trimmed down the project as far as I could while still showing the issue, to allow people to try out ideas/debug if they're interested github:store_import_test

The error happens in: request.js

Note: I'm aware the bounty is about to expire, but I'll re-enable it if that happens. I do appreciate all ideas/help put out so far!

End update 2

Update 1 - purpose description:

I want to access a value from the store (that can change overtime) in a 'utility function'. According to the redux docs subscribe is a valid option.

End update

I'm trying to import my redux-store outside of a component (in request.js , see below) similar to: What is the best way to access redux store outside a react component?

However, all those solutions (including https://github.com/reactjs/redux/issues/776) don't work because my request.js tries to import the store before createStore() is called in store.js resulting in store being undefined.

My directory structure looks like this

 .  
 ├── app  
 │   ├── api  
 │   ├── network 
 │   |    └── request.js 
 │   ├── app.js  
 │   ├── store.js  
 ├── index.android.js  
 ├── index.ios.js

The index.android/ios.js are the entry points and just load app.js

index.android/ios.js

import App from './app/app'

app.js

import store from './store'
class App extends React.Component {
    render() {
        return (
            <Provider store={store}>
                    <RootStackNavigation />
            </Provider>
        )
    }
} 

store.js

...
const store = createStore(reducer, initialState, middleware())
export default store

request.js

import store from '../../store'
store.subscribe(listener)
...
const someFunc(){}
export default someFunc

My thoughts / what I tried / where I get lost

Note: the store import path in request.js is valid, double checked Note2: the store is usable in app.js and the remainder of the program

I would think that the import store from '../../store' in request.js would trigger the const store = createStore(reducer, initialState, middleware()) line but apparently it seems it does.

attempt 1

I tried to also export the store as such:

export const store = createStore(reducer, initialState, middleware())

and imported it in request.js as:

import {store} from '../../store

thinking maybe the 'clever' default loading does some magic I don't know/understand. Same error, undefined

attempt 2 add getStore() to store.js

let store = null
store = createStore(reducer, initialState, middleware())
export function getStore() {
    if (store == null) {
        store = createStore(reducer, initialState, middleware())
    }
    return store
}

export default store

Doesn't work, the arguments to createStore have not been initialised yet.

I must be misunderstanding the loading process or mixing it with that of python, but what's preventing this from working? Others seem to be using the same solution (see the above mentioned posts) successfully.

12
  • I think you are trying to use the state of the react (with Redux management) without using react, which will not happen. Have you consider putting the request as a middleware to the application ? Commented Oct 4, 2017 at 7:38
  • if you look at the SO I linked, you'll see people successfully doing what I'm asking. e.g. this solution stackoverflow.com/a/38480550/3869515 I'm basically trying to do the same as that solution. Given their success I think it should be possible
    – ixje
    Commented Oct 4, 2017 at 8:04
  • in your example the listener function is missing i guess you forgot to paste it right ? Commented Oct 4, 2017 at 8:12
  • I omitted that part because I tried to keep it as minimal as possible without losing too much information. I clarified it by adding '...' in between. The error message is along the lines of "can't call subscribe on undefined" and when debugging it shows that store is undefined.
    – ixje
    Commented Oct 4, 2017 at 8:15
  • Could you provide feedback about listener in request.js? Commented Oct 4, 2017 at 12:10

4 Answers 4

3

Found the problem. I was right - it’s a cycle. In store.js you require reducers/index.js then navigation.js then ../navigators/RootNavigationConfiguration which requires Home.js which requires /api/network which requires request.js and it requires store which at this point is not initialized. Try moving the store.subscribe(listener) along with the listener function to store.js right before you export it. Don’t forget to remove import store.js from request.js

9
  • Good find on the cycle. I don't think it's viable to move all such scenario's to store.js. Also, in my current case the listener() is supposed to operate on data I need in request.js. Even if I move it for just this case, then I'd have to expose the extra data in store.js, import this in request.js and then we have another cycle, right? If you look at request.js then I want my base_url @ line 32 to point to the value of rpcUrl, in turn rpcUrl is to be modified by listener() if network.net in the store changes. Hope that still make sense.
    – ixje
    Commented Nov 5, 2017 at 10:49
  • In short I want to modify the base_url on the fly depending on the network.net value in my store.
    – ixje
    Commented Nov 5, 2017 at 10:50
  • @justsome what i was about to say is that this is anti pattern. Are you using Redux thunk? If so get the url from the store and pass it on to api methods. Another way I recommend is using redux saga and passing it down the api methods too. Commented Nov 5, 2017 at 15:21
  • I'm using redux-saga, but passing the network.net value from the store around for every network call seems ridiculous. The store.subscribe() function is there for a reason. The examples I linked in the main post shows people doing the exact same thing but not for changing the base_url but for adding a token. I guess I should look into how to avoid having to load RootNavigationConfiguration from my navigation reducer (if that's possible)
    – ixje
    Commented Nov 5, 2017 at 17:27
  • How about setting a timeout to store.subscribe()? Commented Nov 5, 2017 at 17:31
0

I feel that you want to make antipattern. Your needcase is whispering to my ears "saga", but let's help you as it is.

I want to give you closed issue from github, but I want to make it clear where "they" are getting store - import {store} from "store"; is exported const store = configureStore(); and configure store (to avoid missunderstanding) is returning redux.createStore(...).

After you know what I wrote - you can understand what "they" are writing there and there.

4
  • 1
    I don't understand the relationship to the linked github issues. I want to access a value from the store (that can change overtime) in a 'utility function'. According to the redux docs subscribe is a valid option. I'm not trying to dispatch actions or modify state like those issues linked try to do.
    – ixje
    Commented Oct 4, 2017 at 8:40
  • I understood it like that you want to create function that will do something on store update Commented Oct 4, 2017 at 9:21
  • No problem. I updated the question to add my extra explanation to hopefully avoid further confusion. store.getState()... does not work in request.js because store is undefined at that point. That's the actual problem I'm facing. Once that's solved I can do whatever I want :)
    – ixje
    Commented Oct 4, 2017 at 11:52
  • Go on and post another question, it's interesting to find out how to implement this Commented Nov 6, 2017 at 13:57
0

Did you write "../../store" in request.js, but your directory structure says it should be "../store".

1
  • You're right. However, it's only wrong in my edit of the tree output as the network folder is actually a subfolder of api. The actual import is correct. I've updated the post to include a minimum example project showcasing the issue.
    – ixje
    Commented Nov 4, 2017 at 9:36
0

You can simply create your store in the App Component to reduce complications, and pass it through the to sub-components.

let store = createStore(reducers);

export class App extends Component {
    constructor() {
        super();
        this.state = store.getState();
        this.syncState = this.syncState.bind(this);
    }

    syncState() {
        this.setState(store.getState());         
    }

    componentDidMount() {
        this.setState(store.getState());
        const unsubscribe = store.subscribe(this.syncState);
    }

    render() {
        return (
            <Provider store={store}>
                <AppNavigator/>
            </Provider>
        )
    }
}

Your sub-components then need to import connect from react-redux.

import {connect} from 'react-redux';

export class SubComponent extends Component {
  //Implementation
  //this.props.myLocalCopy - gives you the value
  //this.props.someTask(data) - Updates the store and updates your props.
}

function mapStateToProps(state) {
    return {
        myLocalCopy: state.someStoreProperty
    };
}

function mapDispatchToProps(dispatch) {
     return {
         someTask: (data) => {
             dispatch(actionCreators.someTask(data));
         }
     }
}

export default connect(mapStateToProps, mapDispatchToProps)(SubComponent)

This will help you to synchronize your app state with store throughout the workflow. Hope that helps! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.