Search code examples
javascriptreactjsfirebasereact-redux-firebase

why it is returning null?


Hi I am trying to learn firebase. Now I am trying to follow what is inside the github doc. Like to gitHub

This is my index.js file

const rfConfig = {}; // optional redux-firestore Config Options

// Your web app's Firebase configuration
const firebaseConfig = {
  apiKey: 'something',
  authDomain: 'something',
  databaseURL: 'something',
  projectId: 'something',
  storageBucket: 'something.com',
  messagingSenderId: 'something',
  appId: '1:something',
  measurementId: 'G-something',
};

firebase.initializeApp(firebaseConfig);

// Initialize Cloud Firestore through Firebase
firebase.firestore();

// Add reduxFirestore store enhancer to store creator
const createStoreWithFirebase = compose(
  reduxFirestore(firebase, rfConfig), // firebase instance as first argument, rfConfig as optional second
)(createStore);

// Add Firebase to reducers
const rootReducer = combineReducers({
  firestore: firestoreReducer,
});

// Create store with reducers and initial state
const initialState = {};
const store = createStoreWithFirebase(rootReducer, initialState);

ReactDOM.render(
  <React.StrictMode>
    <Provider store={store}>
    <App />
    </Provider>
  </React.StrictMode>,
  document.getElementById('root')
);

My folder got 3 components. 1 is for adding todo. 1 is for displaying todo. & the last 1 is combination of the first two

Here is my app component & TodoShow Component

const App = () => {
  return (
    <div>
      <TodoShow/>
    </div>
  )
}

const TodoShow = () => {
    return (
        <div>
            <Todo/>
            {/* <Todos/> */}
        </div>
    )
}

Inside the Todo button component I want to add a new todo when i click on a button

import { useFirebase } from 'react-redux-firebase'

export default function Todo() {
  const firebase = useFirebase()

  function addSampleTodo() {
    const sampleTodo = { text: 'Sample', done: false }
    return firebase.push('todos', sampleTodo)
  }

  return (
    <div>
      <h1>New Sample Todo</h1>
      <button onClick={addSampleTodo}>Add</button>
    </div>
  )
}

But when I click on the button, The app doesn't know firebase.

Here is the photo enter image description here

Is there something i am missing here? I have already install firebase,react-redux-firebase,redux-firestore


Solution

  • You need to render a ReactReduxFirebaseProvider near the top of your component tree. This is what useFirebase is trying to access, so without one, you get undefined.

    const rrfConfig = {
      userProfile: 'users'
      // useFirestoreForProfile: true // Firestore for Profile instead of Realtime DB
    }
    
    const rrfProps = {
      firebase,
      config: rrfConfig,
      dispatch: store.dispatch
      // createFirestoreInstance // <- needed if using firestore
    }
    
    ReactDOM.render(
      <React.StrictMode>
        <Provider store={store}>
          <ReactReduxFirebaseProvider {...rrfProps}>
            <App />
          </ReactReduxFirebaseProvider>
        </Provider>
      </React.StrictMode>,
      document.getElementById('root')
    );
    

    For more info see this section of the documentation: https://github.com/prescottprue/react-redux-firebase#use