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

Error react_context_hook__WEBPACK_IMPORTED_MODULE_12__.store.get is not a function #90

Open
yelnyafacee opened this issue Jul 7, 2022 · 5 comments

Comments

@yelnyafacee
Copy link

I ma getting Error react_context_hook__WEBPACK_IMPORTED_MODULE_12__.store.get is not a function from store.get('something'),

import { useStore, withStore } from 'react-context-hook';
import {store} from 'react-context-hook' //import the raw store


function Category() {

   .....

  if(store.get('something')) {
  
     ....
  
  }

}

can you show an example of how the store.get('something') is used? how do I get one of the store value with a key?

@Spyna
Copy link
Owner

Spyna commented Jul 11, 2022

import` { withStore} from 'react-context-hook'

export default withStore(App)

I think you forgot to use this command in the app.

Why do you have to access the store that way? I recommend to use the hooks provided by this library itself

@yelnyafacee
Copy link
Author

hi, I am using the store hook like:

    const [
        getSearchData,
        setGetSearchData,
        deleteGetSearchData
    ] = useStore('getSearchData', null);


setGetSearchData(getSearchDataFunc);

async function getSearchDataFunc(pageNumber = 1, params = {}) {

  .....

}

but when I called getSearchData function in a child component like:

getSearchData(1, { 'album_type' : '' });

getSearchDataFunc get called but all the parameters (1, { 'album_type' : '' }) failed to pass through into the function

@Spyna
Copy link
Owner

Spyna commented Jul 26, 2022

Did you wrap the root component in export default withStore(App) ?

@yelnyafacee
Copy link
Author

If I wrap only the top most component with withStore(xxx) alone, somehow the useStore data failed to pass to the child components TypeError: Cannot convert undefined or null to object" ( searchData.album_types ), but if I also wrap the child component with withStore() ( export default withStore(Selected_AlbumType); ) the data can be accessed, is this a bug?

child component ( Selected_AlbumType ):


import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { useStore, withStore, store } from 'react-context-hook';
import { urlGetParam, urlHasParam } from '../../../../utils/Helpers.js';


function Selected_AlbumType() {

    // Declare Global Store State:
    // const [searchData] = useStore('searchData', null);
    // const [getSearchData] = useStore('getSearchData', null);
    // const [album_type] = useStore('album_type', null);

    const [
        searchData,
        setSearchData,
        deleteSearchData
    ] = useStore('searchData', []); 

    const [
        getSearchData,
        setGetSearchData,
        deleteGetSearchData
    ] = useStore('getSearchData', null);

    const [
        query_album_type,
        setQuery_album_type,
        deleteQuery_album_type
    ] = useStore('query_album_type', null);



    /*
    |--------------------------------------------------------------------------
    | Handle : Close Click
    |--------------------------------------------------------------------------
    */
    const handleCloseClick = (index) => {
        getSearchData(1, {}, { 'album_type' : '' }, true);
    }

    /*
    |--------------------------------------------------------------------------
    | JSX : Logic
    |--------------------------------------------------------------------------
    */
    let album_types = {};

    // Search Data Exist
    if(searchData) {

        // Get Album Types from Search Data
        album_types = searchData.album_types;

    }

    const result_jsx = [];

    // Selected > Album Size Exist
    if(urlHasParam('album_type') || (query_album_type)) {

        // Loop Album Size(s)
        for (const [index, [key, value]] of Object.entries(album_types)) {

            // Album Size == Selected Params > Album Size
            if (key === urlGetParam('album_type') || (key === query_album_type)) {

                // Push > JSX
                result_jsx.push(

                    <div key={'album_type'}>

                        <h3 className="section-sidebar-filter-name">Album Type</h3>

                        <div className="section-sidebar-filter-box c-row-gut-1">

                                    <span className="section-sidebar-filter-icon c-textual-button c-link--white" onClick={(e) => handleCloseClick(key)}>

                                      <svg style={{width: '20px', height: '20px', position: 'relative'}}>
                                        <use xlinkHref='/assets/icons/master.svg?v=130#close' style={{fill: 'currentcolor'}} />
                                      </svg>

                                    </span>

                            <h4 className="section-sidebar-filter-selected">{value}</h4>

                        </div>

                    </div>
                )

            }

        }

    }


    /*
    |--------------------------------------------------------------------------
    | Return
    |--------------------------------------------------------------------------
    */
    return result_jsx;

}

// export default Selected_AlbumType;
export default withStore(Selected_AlbumType);

@Spyna
Copy link
Owner

Spyna commented Jul 27, 2022

Ok, if you use withStore as in the example below, using the config and the initial state, you can enable the debug in the console of the browser. To see what is going on in the store.
Furthermore, you can configure the initialValue value of the store, in order to set the base initial value of the state. If you do so, maybe you can resolve the null and undefined problems.
Keep in mind that if you don't set any initial value, the state of the store will always be an empty object.


const storeConfig = {
 logging: true
}
const initialState = {};

export default withStore(App, initialState, storeConfig)

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