-
-
Notifications
You must be signed in to change notification settings - Fork 555
feat(reacthook) React hook API #684
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
Conversation
Codecov Report
@@ Coverage Diff @@
## next #684 +/- ##
==========================================
+ Coverage 82.25% 82.98% +0.73%
==========================================
Files 26 30 +4
Lines 924 964 +40
Branches 166 168 +2
==========================================
+ Hits 760 800 +40
Misses 164 164 |
As all the checks have passed and you also seem in conjunction with them, i would really request you @prescottprue to please merge them to the next branch i am really waiting for this, as it will remove the use of firestoreConnect completely and we can move on to more newer hook's culture introduced by React. The below question i'm not sure whether it belongs here but i will ask anyway as it's a suggestion, please feel free to tell me to start a new thread / issue for it instead. |
@samsoul16 Thanks for the reminder! Was actually starting to look into some stuff for Also glad you are bringing up the |
Hello @prescottprue, This can be a silly question. It seems I'm still on alpha 11
which is in conjunction with https://www.npmjs.com/package/react-redux-firebase this i guess. |
* fix(docs): remove remaining instances of getFirebase from docs - #635 (#694) * feat(core): Start React hook API - #684 - @illuminist
Hello @prescottprue , first of all i would like to really thank you for speeding up things so quick and pushing this so i could get this from npm. Also i would like to thank @illuminist for this amazing idea and work. But now I'm stuck at a situation and would like to use some help. I'm sharing my codepens below. You can only look at the files In this codepen i haven't passed the firestore object to actions but i did it in my real project as that was the only way to get it working. Just in case, this is the working version where i used to pass props which had firestore object when i was using withFirestore. React Redux Firestore with firestoreConnect HOC If you guys want i can also share the code here. |
@samsoul16 Imagine a value from a hook is same as value get from HOC props. With this you can do the exactly same way as your HOC example, https://codesandbox.io/s/o5z54q5829. Notice that instead of props for the first parameter of action creator, I selectively input only firestore as a prop, but it's called hook dependency. With this you can use same action creator as HOC example without any modification which is a main idea for me to create these hooks. Also notice that I also use I noticed that your hook example still has Also instead of |
Hello @illuminist, first of all thank you so much for your time in explaining everything and going to the extend of forking my code-pen and correcting it for helping me out. Here is my modified Code Pen : https://codesandbox.io/s/8423o1m529 I learned a lot from your answer and would like to point things out.
Question : The Key thing here is that even tough now if
In ActionCreator
Thank You for your time and contributions @illuminist & @prescottprue |
@samsoul16 Answer on 4. Creating an empty object has negligible overhead. Let's say almost of js's immutability relies on creating a new object every times a function run. If that concerns you, you can pass Note on 5. The latest commit from Feel free to ask if you have more questions. |
@illuminist Thanks for the prompt reply. For 4. I think i might have not stated it correctly and I'm sorry for that, by overhead i meant overhead for the developer to write empty props but not on the performance. I was not pointing to the performance at all. What i meant was Eg i created 2 action creators with same function defn with props as 1st param and data as 2nd. And as a consistent pattern i followed it throughout
Consider
Noticed that empty
New dispatch calls, notice we didn't pass any props to
This might lead to a more consistent pattern which can be used throughout actionCreators i suppose. UPDATE
Here i just need the For 5. i just noticed they removed the deps array for Is the below code a viable option
Or will this lead to major re-rendering issues? |
Hey folks, just wanted to include a thought - we could possible use the action creators that already exist on the firebase instance by just passing dispatch, we would just need that instance. Glad to see discussion on this! |
@prescottprue One more thing, did you think about using
Also any thoughts on the above discussion. I'm also eagerly waiting for @illuminist to reply. :) |
@samsoul16 I thought I replied yesterday before I getting back home, but turn out didn't and lost that reply ;(. In my project, an action creator has single parameter to be whatever required to run an action. For example, In your example, actually if a function doesn't use second argument, addition props in this case, you can just skip the second argument in function definition, export const createTodo = ({ firebase, todoTitle, todoContent }) => {
return dispatch => { ... }
} For const todoSelector = useCallback(state => state.todos[props.todoId], [props.todoId])
const todo = useSelector(todoSelector) This ensure the
const add = (a, b) => {
return a + b
}
console.log(add(2)) This doesn't throw an error. The function still return a result, which is |
@illuminist That's a great proposal man. I love it and would go with this pattern now, where
and then all the above use cases can be solved as follows
|
Hello @illuminist can you please tell me how you solved this Warning/ Error
the |
const signUpUser = useCallback(
details => dispatch(signUp({ details, firebase, firestore })),
[firebase, firestore, dispatch]
); Neither firebase nor firestore instance change through time. The usage of |
Ah!! not the cleanest of things, again an overhead to pass it around, but for now i will be doing that, Thank you |
Description
I introduced new 4 apis that use react hook.
useFirebase
useFirebaseConnect
useFirestore
useFirestoreConnect
useFirebase
anduseFirestore
Basically just returning firebase and firestore instance accordingly. Internally uses
useContext
hook to get the instance fromReactReduxFirebaseContext
useFirebaseConnect
anduseFirestoreConnect
Almost exactly same as firebaseConnect and firestoreConnect HOC, except:
The reason of this is
useEffect
hook has to memoize data dependency. If there are multiple queries in a hook.useEffect
will clean up(unset) all the queries in it even only one query has been changed, leading to unnecessary unset/set listener.And we cannot use multiple
useEffect
hook on an array of query. "Rules of hook" state that hook should not be use in a loop or conditionally assign.firestoreConnect
query), the query needed to be memoized before being applied touseFirestoreConnect
. If different query object, even one with exactly same value, will cause hook to unset/set listener.Example
Note:
useFirebaseConnect
anduseFirestoreConnect
will not accept array of query anymoreTo listen to multiple queries, use multiple connector hooks
Dependency
react
version needed to be upgrade to 16.8 which provide hook APICheck List
Relevant Issues