question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FirestoreReducer Typescript not working as expected

See original GitHub issue

What is the current behavior?

Using Typescript, I wanted to get access to the properties of my firestore data. Reading the docs, I realized it was possible with firebase, but notmention of firestore. I tried to implement it and when I created my RootState interface, I noticed a couple of things:

  1. There is no FirestoreReducer.Reducer implemented in redux-firestore. I only found it in react-redux-firebase.
  2. In the FirestoreReducer in react-redux-firebase, I noticed there was no Schema type that could be implemented. I don’t know if it’s normal, because we need to pass in our own Schema so it knows what to expect when you fetch the data. This works fine with the FirebaseReducer.
  3. So I tried something out. I am new to Typescript, so it might not be proper, or maybe I don’t understand something fundamental :

I added FirestoreReducer to redux-firestore, and added a Schema type. Also copied over the data and ordered types from FirebaseReducer :

export namespace FirestoreReducer {
  export interface Reducer<Schema extends Record<string, any> = {}> {
    composite?: Data<any | Dictionary<any>>;
    data: { [T in keyof Schema]: Record<string, Schema[T]> };
    errors: {
      allIds: string[];
      byQuery: any[];
    };
    listeners: Listeners;
    ordered: {
      [T in keyof Schema]: Array<Schema[T]>;
    };
    queries: Data<ReduxFirestoreQuerySetting & (Dictionary<any> | any)>;
    status: {
      requested: Dictionary<boolean>;
      requesting: Dictionary<boolean>;
      timestamps: Dictionary<number>;
    };
  }

  const prototype: {};
}

My Schema and RootState :

interface Schema {
  programs: Program;
}
export interface RootState {
  firebase: FirebaseReducer.Reducer<UserAccount, Schema>;
  firestore: FirestoreReducer.Reducer<Schema>;
}

I also noticed something else : ordered: { [T in keyof Schema]: Array<{ key: string; value: Schema[T] }> }. I don’t know if this is intended, but with this I don’t get the right data unless you specify .value when you fetch your data. For example :

const programs = useSelector((state: RootState) => state.firestore.ordered.programs);
programs.map(p => p.value.ProgramField)

Changing this to ordered: {[T in keyof Schema]: Array<Schema[T]>; }; fixes this. With these changes, I can now call

programs.map(p => p.ProgramField)

Am I doing this right? Or is there already a way to do it properly with the current version? Appreciate any help!

Which version of redux-firestore are you using? What about other dependencies? react-redux-firebase: 3.6.0 redux-firestore: 0.13.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
prescottpruecommented, Feb 15, 2021

For some reason it was storing just as a draft - published now

0reactions
0xdevaliascommented, Feb 15, 2021

Please reach out if things aren’t working as expected

@prescottprue That link 404’s, seems there is no v0.15.0 release yet?

Read more comments on GitHub >

github_iconTop Results From Across the Web

redux-firestore/Lobby - Gitter
hey @prescottprue I'm running into some issues with the firestore reducer ... In particular, the .data member of the firestore reducer clears as...
Read more >
Firestore Timestamp not being recognized as such in the ...
It looks like this format of the Timestamp is working as expected: A Timestamp represents a point in time independent of any time...
Read more >
Typescript: firestore - No overload matches this call.
Type 'typeof firestoreReducer' is not assignable to type 'Reducer<Reducer ... 5): The expected type comes from property 'firestore' which is ...
Read more >
Usage With TypeScript - Redux Toolkit
Details on how to use each Redux Toolkit API with TypeScript ... If you encounter any problems with the types that are not...
Read more >
React 18, React Redux 8, and TypeScript: What you need to ...
Get notified of impactful user issues, not false positives. Less alerts, way more useful signal. The LogRocket Redux middleware package adds an ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found