|
1 | 1 | # Write
|
2 | 2 |
|
| 3 | +Writing to the Firestore is simple too! All you need to do is import a hook that you want to use and initialize it with a string pointing to the collection you'll do the operation on. |
| 4 | +You'll always get an array of: |
| 5 | +- `loading` state, that indicates if the process is underway. Useful for disabling a button after submission or showing a spinner. |
| 6 | +- `error` which by default is `null`, but when there is a problem with be populated with an error message created by firebase |
| 7 | +- `function` that does the action that you requested, by calling chosen hook |
| 8 | + |
| 9 | +All of the hooks take `collection` string as a parameter. Have a look at the examples below for more in detail information about that, and occasional extra parameters. |
| 10 | + |
3 | 11 | All of the mutations get timestamps attached to them by default so you don't have to worry about them
|
4 | 12 |
|
5 |
| -## Set |
| 13 | +Also all of the mutation hooks support passing in a string of `__user` to indicate that you want to replace it with currently logged in user id. [Read more about how that works here](./other.md) |
| 14 | + |
| 15 | +## `useSet` |
| 16 | + |
| 17 | +Use when you have an id of thing you want to change or if you want to create an item with given id. |
| 18 | + |
| 19 | +`useSet` takes two arguments. Besides, as always, a `collection` string, it also takes an optional `merge` boolean (default `false`) that specifies if you want to keep previous values or overwrite the whole document with passed in object. |
| 20 | + |
| 21 | +The `setFunction` it returns takes two arguments: |
| 22 | +- `uid` of the document you want to set |
| 23 | +- `data` object you want to set it to |
| 24 | + |
| 25 | +### Example |
6 | 26 |
|
7 | 27 | ```jsx
|
8 |
| -// Initialize the hook with parameters |
9 |
| -// first is operation, which is a type of change you want to do on the Firestore |
10 |
| -// second is a name of the collection in Firestore you want to change |
11 |
| -// then there is an object for all of the options - always consult docs if you want to use them |
12 |
| -// as they change for different operations, though if you don't pass them they all have defaults |
13 |
| - |
14 |
| -// pro tip - its helpful to read the hook like this: |
15 |
| -// const setCity = useWriteFS('set', {/* document in */} 'city', { |
16 |
| - |
17 |
| -const setCity = useWriteFS('set', 'city', { |
18 |
| - merge: true, // default false |
19 |
| -}); |
20 |
| - |
21 |
| -// Example in a submit action: |
22 |
| -// Function needs to be async as the setCity returns a promise |
23 |
| -handleSubmit = async () => { |
24 |
| - // we need a try catch block in this async function as writing to db is done over the network, |
25 |
| - // and stuff can always go wrong |
26 |
| - try { |
27 |
| - // need to await the operation to resolve successfully |
28 |
| - // function doesn't return anything so no need to capture the outcome |
29 |
| - await setCity({data: 'that', you: 'want', to: 'save'}, 'uidOfTheDocYouWantToChangeOrCreate') |
30 |
| - // after its done you can call any other function like redirect to other path: |
31 |
| - props.history.push('/') |
32 |
| - } catch (e) { |
33 |
| - // handle the error. Might have been caused by collection not existing for example |
34 |
| - } |
35 |
| -} |
| 28 | +import React, { useState } from 'react'; |
| 29 | +import { useSet } from 'firebase-hooks-react'; |
| 30 | + |
| 31 | +const App = () => { |
| 32 | + // we need to point the hook to the collection with the parameter string |
| 33 | + const [loading, error, setName] = useSet('names', {/* optional merge boolean */} true); |
| 34 | + const [name, setNameField] = useState('') |
| 35 | + |
| 36 | + const onSubmit = (e) => { |
| 37 | + e.preventDefault(); |
| 38 | + // then we can use it wherever we want, like in this submit function |
| 39 | + // that sends an object to the database with key of name and value of name (thats in the state) |
| 40 | + setName('testing', { name }); |
| 41 | + }; |
| 42 | + |
| 43 | + return ( |
| 44 | + <div className='App'> |
| 45 | + <form onSubmit={onSubmit}> |
| 46 | + <input type='text' value={name} onChange={e => setNameField(e.target.value)} /> |
| 47 | + {/* useful thing is using loading boolean for disabling the button */} |
| 48 | + <input type='submit' value='Change your name' disabled={loading} /> |
| 49 | + {/* you should probably do some more error handling than this, but its quick and dirty and does the work here */} |
| 50 | + {error && JSON.stringify(error)} |
| 51 | + </form> |
| 52 | + </div> |
| 53 | + ); |
| 54 | +}; |
| 55 | + |
| 56 | +export default App; |
36 | 57 | ```
|
37 | 58 |
|
38 |
| -## Add |
| 59 | +## `useAdd` |
| 60 | + |
| 61 | +Use when you don't want to think about what id you want to give something, you just want to add a new doc to the collection. Only takes a `collection` string. |
| 62 | + |
| 63 | +The `addFunction` that the hook returns takes one argument: |
| 64 | +- `data` object containing the data you want to add. |
| 65 | + |
| 66 | +### Example |
39 | 67 |
|
40 | 68 | ```jsx
|
41 |
| -// same process as with set, so for details have a look above |
42 |
| -// same pro-tip as above - its useful to read it like this: |
43 |
| -// const setCity = useWriteFS('add', {/* document to */} 'city') |
44 |
| - |
45 |
| -// This time there are no options as we let firestore figure out the id |
46 |
| -const addCity = useWriteFS('add', 'city'); |
47 |
| - |
48 |
| -// Example in a submit action: |
49 |
| -// Function needs to be async as the addCity returns a promise |
50 |
| -handleSubmit = async () => { |
51 |
| - try { |
52 |
| - // need to await the operation to resolve successfully |
53 |
| - // function doesn't return anything so no need to capture the outcome |
54 |
| - await addCity({data: 'that', you: 'want', to: 'save'}) |
55 |
| - // after its done you can call any other function like redirect to other path: |
56 |
| - props.history.push('/') |
57 |
| - } catch (e) { |
58 |
| - // handle the error. Might have been caused by collection not existing for example |
59 |
| - } |
60 |
| -} |
| 69 | +import React, { useState } from 'react'; |
| 70 | +import { useAdd } from 'firebase-hooks-react'; |
| 71 | + |
| 72 | +const App = () => { |
| 73 | + // we need to point the hook to the collection with the parameter string |
| 74 | + const [loading, error, addName] = useAdd('names'); |
| 75 | + const [name, setName] = useState('') |
| 76 | + |
| 77 | + const onSubmit = (e) => { |
| 78 | + e.preventDefault(); |
| 79 | + // then we can use it wherever we want |
| 80 | + addName({ name }); |
| 81 | + }; |
| 82 | + |
| 83 | + return ( |
| 84 | + <div className='App'> |
| 85 | + <form onSubmit={onSubmit}> |
| 86 | + <input type='text' value={name} onChange={e => setName(e.target.value)} /> |
| 87 | + {/* useful thing is using loading boolean for disabling the button */} |
| 88 | + <input type='submit' value='Add new name' disabled={loading} /> |
| 89 | + {error && JSON.stringify(error)} |
| 90 | + </form> |
| 91 | + </div> |
| 92 | + ); |
| 93 | +}; |
| 94 | + |
| 95 | +export default App; |
61 | 96 | ```
|
62 | 97 |
|
63 |
| -## Update |
| 98 | +## `useUpdate` |
| 99 | + |
| 100 | +Useful if you have a document that you know the id of and you want to update one field in it. Pretty much the same as using the `useSet` hook with merge set to true. Takes only a `collection` string. |
| 101 | + |
| 102 | +The `updateFunction`, that the hook returns, takes two arguments: |
| 103 | +- `uid` of the document you want to update |
| 104 | +- `data` object with fields that you want to update in a document |
64 | 105 |
|
65 | 106 | ```jsx
|
66 |
| -// Very similar structure to Set function, the difference is only in the string that you have to pass, |
67 |
| -// and that there are no extra options to pass |
68 |
| -// a reading pro-tip again: |
69 |
| -// const setCity = useWriteFS('update', {/* document in */} 'city') |
70 |
| -const updateCity = useWriteFS('update', 'city'); |
71 |
| - |
72 |
| -// Example in a submit action: |
73 |
| -// Function needs to be async as the updateCity returns a promise |
74 |
| -handleSubmit = async () => { |
75 |
| - try { |
76 |
| - // we need to know what document you want to update - by passing its id as first arg |
77 |
| - // second parameter is an object of existing values you want to update |
78 |
| - // consult with these docs on what you can actually do with that as there are cool things like increase by one |
79 |
| - // https://firebase.google.com/docs/firestore/manage-data/add-data#update-data |
80 |
| - await updateCity({data: 'that', you: 'want', to: 'save'}, 'uidOfTheDocYouWantToChangeOrCreate') |
81 |
| - // after its done you can call any other function like redirect to other path: |
82 |
| - props.history.push('/') |
83 |
| - } catch (e) { |
84 |
| - // handle the error. Might have been caused by collection not existing for example |
85 |
| - } |
86 |
| -} |
| 107 | +import React, { useState } from 'react'; |
| 108 | +import { useUpdate } from 'firebase-hooks-react'; |
| 109 | + |
| 110 | +const App = () => { |
| 111 | + // we need to point the hook to the collection with the parameter string |
| 112 | + const [loading, error, updateName] = useUpdate('names'); |
| 113 | + const [name, setName] = useState('') |
| 114 | + |
| 115 | + const onSubmit = (e) => { |
| 116 | + e.preventDefault(); |
| 117 | + // then we can use it wherever we want |
| 118 | + updateName('testing', { name }); |
| 119 | + }; |
| 120 | + |
| 121 | + return ( |
| 122 | + <div className='App'> |
| 123 | + <form onSubmit={onSubmit}> |
| 124 | + <input type='text' value={name} onChange={e => setName(e.target.value)} /> |
| 125 | + {/* useful thing is using loading boolean for disabling the button */} |
| 126 | + <input type='submit' value='Change your name' disabled={loading} /> |
| 127 | + {error && JSON.stringify(error)} |
| 128 | + </form> |
| 129 | + </div> |
| 130 | + ); |
| 131 | +}; |
| 132 | + |
| 133 | +export default App; |
87 | 134 | ```
|
88 | 135 |
|
89 |
| -## Delete |
| 136 | +## `useDelete` |
| 137 | + |
| 138 | +Simply deletes a document in the firebase, based on the id that you give it. Takes a `collection` string as always. |
| 139 | + |
| 140 | +The `deleteFunction`, that the hook returns, takes one argument: |
| 141 | +- `uid` of the document you want to delete |
90 | 142 |
|
91 | 143 | ```jsx
|
92 |
| -// You might get the theme that these hooks are meant to look very similar and no changes here |
93 |
| -// one thing you have to keep in mind that deleting a document doesnt delete its children, |
94 |
| -// you can read about it here: https://firebase.google.com/docs/firestore/manage-data/delete-data#collections |
95 |
| -const deleteCity = useWriteFS('delete', {/*document in*/} 'city'); |
96 |
| - |
97 |
| -// Example in a submit action: |
98 |
| -// Function needs to be async as the updateCity returns a promise |
99 |
| -handleSubmit = async () => { |
100 |
| - try { |
101 |
| - // we need to know what document you want to delete - we do that by passing its id as first arg |
102 |
| - // It doesnt return anything |
103 |
| - await deleteCity('uidOfTheDocYouWantToChangeOrCreate') |
104 |
| - // after its done you can call any other function like redirect to other path: |
105 |
| - props.history.push('/') |
106 |
| - } catch (e) { |
107 |
| - // handle the error. Might have been caused by collection not existing for example |
108 |
| - } |
109 |
| -} |
| 144 | +import React from 'react'; |
| 145 | +import { useDelete } from 'firebase-hooks-react'; |
| 146 | + |
| 147 | +const App = () => { |
| 148 | + // we need to point the hook to the collection with the parameter string |
| 149 | + const [loading, error, deleteName] = useDelete('names'); |
| 150 | + |
| 151 | + const handleClick = () => { |
| 152 | + // then we can use it wherever we want |
| 153 | + deleteName('testing'); |
| 154 | + }; |
| 155 | + |
| 156 | + return ( |
| 157 | + <button value='Delete some name' onClick={handleClick} /> |
| 158 | + ); |
| 159 | +}; |
| 160 | + |
| 161 | +export default App; |
110 | 162 | ```
|
111 | 163 |
|
112 |
| -## Delete a field |
| 164 | +## `useDeleteFields` |
| 165 | + |
| 166 | +Only deletes specified fields from a chosen document |
| 167 | + |
| 168 | +The `deleteFieldsFunction`, that the hook returns, takes two arguments: |
| 169 | +- `uid` of the document you want to update |
| 170 | +- `fields` which is an array of strings of fields that you want to delete in a document |
113 | 171 |
|
114 | 172 | ```jsx
|
115 |
| -// probably the least useful but still here |
116 |
| -// similar to delete action, just accepts an extra argument which is an array of fields to delete |
117 |
| -const deleteFieldInCity = useWriteFS('deleteField', {/*in a document in the*/} 'city'); |
118 |
| - |
119 |
| -// Example in a submit action: |
120 |
| -// Function needs to be async as the deleteFieldInCity returns a promise |
121 |
| -handleSubmit = async () => { |
122 |
| - try { |
123 |
| - // we need to know in what document you want to delete passed fields - we do that by passing its id as first arg |
124 |
| - // then we need to know what fields you want to delete, pass them as an array of strings |
125 |
| - // It doesn't return anything |
126 |
| - await setCity('uidOfTheDocYouWantToChangeOrCreate', ['capital', 'population']) |
127 |
| - // after its done you can call any other function like redirect to other path: |
128 |
| - props.history.push('/') |
129 |
| - } catch (e) { |
130 |
| - // handle the error. Might have been caused by collection not existing for example |
131 |
| - } |
132 |
| -} |
| 173 | +import React from 'react'; |
| 174 | +import { useDeleteFields } from 'firebase-hooks-react'; |
| 175 | + |
| 176 | +const App = () => { |
| 177 | + // we need to point the hook to the collection with the parameter string |
| 178 | + const [loading, error, deleteFieldsInName] = useDelete('names'); |
| 179 | + |
| 180 | + const handleClick = () => { |
| 181 | + // then we can use it wherever we want |
| 182 | + deleteFieldsInName('testing', ['name']); |
| 183 | + }; |
| 184 | + |
| 185 | + return ( |
| 186 | + <button value='Delete some fields in name' onClick={handleClick} /> |
| 187 | + ); |
| 188 | +}; |
133 | 189 |
|
| 190 | +export default App; |
134 | 191 | ```
|
0 commit comments