简体   繁体   中英

ProviderFunction not exporting my new functions

My Problem: I expect my FirebaseProvider function to provide an object containing all functions, through the app. The problem is that all functions are well provided through my files, except my last new function: fetchTest .

Explainations: If I click the TestPage.js button I get Uncaught TypeError: fetchTest is not a function .
I saw many posts on stackoverflow about this type of error, but none did help me. -> I think the original problem is the index.js is not called. The console.log("firebaseprovider") (in index.js ) does not appear in console, yet the other files of the project in web-app/src/views/ have the same imports and exports than TestPage .

Since App.js code worked fine on all the other files, I don't know how console.log("firebaseprovider") is never displayed in the navigator console. ( edit : no matter which page I go, this console.log never appears)
<FirebaseProvider> seems to not provide TestPage.js . Do you have an idea?

What I've tried:

  • placing a console.log in TestPage.js : it shows every function written in index.js but not fetchTest . It seems to not be properly exported through api object.
  • in TestPage.js trying console.log("api.fetchTest") : console displays undefined .
  • add a second testing function in index.js , whithout parameters, which just does console.log("test")
  • compare imports / exports and api declarations with other files in web-app/src/views/
  • create a handleSubmit() function in TestPage.js to not put the functions directly in return
  • delete node_modules and then yarn install
  • yarn workspace web-app build and then relaunch yarn workspace web-app start

(This is a Yarn Workspaces project containing a common/ and a web-app/ folders)

common/src/ index.js :

import React, { createContext } from 'react';
import {FirebaseConfig} from 'config';

const FirebaseContext = createContext(null);
const FirebaseProvider  = ({ children }) => {
    console.log("firebaseprovider");  // is not displayed in the console
    let firebase = { app: null, database: null, auth: null, storage:null }

    if (!app.apps.length) {  // I tried to comment out this line (and the '}') -> no difference
        app.initializeApp(FirebaseConfig);  // no difference when commented out
        firebase = {
            app: app,
            database: app.database(),
            auth: app.auth(),
            storage: app.storage(),
            // [ ... ] other lines of similar code
            api : {  // here are functions to import
              fetchUser: () => (dispatch) => fetchUser()(dispatch)(firebase),
              addProfile: (details) => (dispatch) => addProfile(userDetails)(dispatch)(firebase),
              // [ ... ] other functions, properly exported and working in other files
              // My function :
              fetchTest: (testData) => (dispatch) => fetchTest(testData)(dispatch)(firebase),
            }
        }
    }
    return (
        <FirebaseContext.Provider value={firebase}>
            {children}
        </FirebaseContext.Provider>
    )
}
export { FirebaseContext, FirebaseProvider, store }

web-app/src/views/ TestPage.js :

import React, { useContext } from "react";
import { useDispatch } from "react-redux";
import { FirebaseContext } from "common";

const TestPage.js = () => {
  const { api } = useContext(FirebaseContext);
  console.log(api);  // Displays all functions in api object, but not fetchTest
  const { fetchTest } = api;
  const dispatch = useDispatch();
  const testData = { validation: "pending" };
  return <button onClick={ () => {
    dispatch(fetchTest(testData));  // Tried with/without dispatch
    alert("done");
  }}>Test button</button>
}
export default TestPage;

web-app/src/ App.js :

import React from 'react';
import { Router, Route, Switch } from 'react-router-dom';
// ... import all pages
import { Provider } from 'react-redux';
import TestPage from './views/CreateSiteNeed';  // written same way  for the other pages
import { store, FirebaseProvider } from 'common';
function App() {
  return (
    <Provider store={store}>
      <FirebaseProvider>
        <AuthLoading>
          <Router history={hist}>
            <Switch>
              <ProtectedRoute exact component={MyProfile} path="/profile" />
              <!-- [ ... ] more <ProtectedRoute /> lines, form imported Pages line 3. -->
              <ProtectedRoute exact component={TestPage} path="/testpage" />
            </Switch>
          </Router>
        </AuthLoading>
      </FirebaseProvider>
    </Provider>
  );
}
export default App;

I hope some people will find this post helpful, thanks

Here was the problem:

Firstly:
I'm using Redux, so fetchTest() has its testActions.js and testReducer.js files, which are functionnal. But I did forget to update my store.js :

// [ ... ] import all reducers
import { testReducer as testData } from '../reducers/testReducer'; // was'nt imported

const reducers = combineReducers({
  auth,
  usersdata,
  // [ ... ] other imported reducers
  testData  // My test reducer
}
// The rest is a classic store.js code

Secondly:
As I'm using Yarn Workspaces, I had to compile the code in common/dist/index.js to make it accessible through the whole entire code (even for local testing).
Here is the command to compile the code (-> to include all redux edits made above) and make it accessible to web-app workspace:

yarn workspace common build && yarn workspace web-app add common@1.0.0 --force

Explanations on the second part of the command ( yarn workspace web-app add common@1.0.0 --force ):
The web-app/package.json file contains { "dependencies": {... "common":"1.0.0"... }}

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