Skip to content

Commit 78b14d7

Browse files
authored
v2.0.0-beta.19 (prescottprue#356)
* feat(firestoreConnect): support `componentWillReceiveProps` in `firestoreConnect` (props/query params change) - prescottprue#354 - @danleavitt0 * feat(docs): actions docs updated with `recompose` examples (for more simple functional components) * fix(docs): profile docs updated with new wording (clarifies usage) * fix(build): `codecov` npm script updated to fix warning
1 parent f1b3595 commit 78b14d7

File tree

5 files changed

+124
-45
lines changed

5 files changed

+124
-45
lines changed

docs/recipes/actions.md

Lines changed: 108 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
# Actions
2-
react-redux-firebase comes with built in async action creators for all parts of Firebase including storage, auth, Real Time Database, and Firestore (firestore requires extra setup).
32

4-
For more on what [an async action creator is](http://redux.js.org/docs/advanced/AsyncActions.html#async-action-creators), please visit the [section on it in the redux-docs](http://redux.js.org/docs/advanced/AsyncActions.html#async-action-creators)
3+
react-redux-firebase comes with built in async action creators for all parts of Firebase including storage, auth, Real Time Database, and Firestore (firestore requires extra setup). These action creators dispatch actions which are then handled by the reducers. The examples below show using action creators as promises, but it is also possible to use redux state.
4+
5+
For more on what [an async action creator is](http://redux.js.org/docs/advanced/AsyncActions.html#async-action-creators), please visit the [section on it in the redux-docs](http://redux.js.org/docs/advanced/AsyncActions.html#async-action-creators).
56

67
## Components
78
Firebase actions can be accessed within a component by using either the [`withFirebase`](/docs/api/withFirebase) wrapper or the [`firebaseConnect` wrapper](/docs/api/firebaseConnect) like so:
89

9-
#### Pure Component
10+
#### Functional Component
1011
```js
1112
import React from 'react'
1213
import PropTypes from 'prop-types'
1314
import { firebaseConnect, withFirebase } from 'react-redux-firebase'
1415

15-
const SimpleComponent = () => (
16-
<button onClick={() => this.props.firebase.push('todos', { some: 'data' })}>
16+
const SimpleComponent = (props) => (
17+
<button onClick={() => props.firebase.push('todos', { some: 'data' })}>
1718
Test Push
1819
</button>
1920
)
@@ -29,9 +30,41 @@ export default withFirebase(SimpleComponent)
2930
// export default firebaseConnect()(SimpleComponent)
3031
```
3132

32-
#### Stateful Component
33+
When using functional components, [recompose](https://github.com/acdlite/recompose/blob/master/docs/API.md) is a nice utility (think of it like lodash for Functional React Components):
34+
35+
```js
36+
import React from 'react'
37+
import PropTypes from 'prop-types'
38+
import { compose } from 'recompose'
39+
import { withFirebase } from 'react-redux-firebase'
40+
41+
const SimpleComponent = ({ createTodo }) => (
42+
<button onClick={createTodo}>
43+
Test Push
44+
</button>
45+
)
46+
47+
SimpleComponent.propTypes = {
48+
firebase: PropTypes.shape({
49+
push: PropTypes.func.isRequired
50+
})
51+
}
52+
53+
export default compose(
54+
withFirebase,
55+
withHandlers({
56+
createTodo: props => event => {
57+
return props.firebase.push('todos', { some: 'data' })
58+
}
59+
})
60+
)(SimpleComponent)
61+
// firebaseConnect can also be used (helpful for creating listeners at the same time)
62+
// export default firebaseConnect()(SimpleComponent)
63+
```
64+
65+
#### Stateful Components
3366

34-
**Wrapping A Component**
67+
**Wrapping A Class Component**
3568

3669
```js
3770
import React, { Component } from 'react'
@@ -59,9 +92,12 @@ class SimpleComponent extends Component {
5992

6093
render() {
6194
return (
62-
<button onClick={this.testPush}>
63-
Test Push
64-
</button>
95+
<div>
96+
<span>Was sent: {this.state.wasSent}</span>
97+
<button onClick={this.testPush}>
98+
Test Push
99+
</button>
100+
</div>
65101
)
66102
}
67103
}
@@ -71,20 +107,19 @@ class SimpleComponent extends Component {
71107

72108
Or if you are using decorators, you can accomplish the same thing with
73109
```js
74-
@firebaseConnect()
110+
@firebaseConnect() // @withFirebase can also be used
75111
export default class SimpleComponent extends Component {
76112
// same component code from above
77113
}
78114
```
79115

80-
**Context Types**
116+
**Directly From Context**
81117

82118
`react-redux` passes store through `context` using `<Provider>`, so you can grab `store.firebase`:
83119

84120
```js
85121
import React, { Component } from 'react'
86122
import PropTypes from 'prop-types'
87-
import { firebaseConnect } from 'react-redux-firebase'
88123

89124
export default class SimpleComponent extends Component {
90125
static contextTypes = {
@@ -105,14 +140,71 @@ export default class SimpleComponent extends Component {
105140

106141
render() {
107142
return (
108-
<button onClick={this.testPush}>
109-
Test Push
110-
</button>
143+
<div>
144+
<span>Was sent: {this.state.wasSent}</span>
145+
<button onClick={this.testPush}>
146+
Test Push
147+
</button>
148+
</div>
111149
)
112150
}
113151
}
114152
```
115153

154+
**Functional Stateful**
155+
156+
```js
157+
import React from 'react'
158+
import PropTypes from 'prop-types'
159+
import { compose, withStateHandlers, withHandlers } from 'recompose'
160+
import { withFirebase } from 'react-redux-firebase'
161+
162+
const SimpleComponent = ({ createTodo, wasSent }) => (
163+
<div>
164+
<span>Was sent: {wasSent}</span>
165+
<button onClick={createTodo}>
166+
Test Push
167+
</button>
168+
</div>
169+
)
170+
171+
SimpleComponent.propTypes = {
172+
firebase: PropTypes.shape({
173+
push: PropTypes.func.isRequired
174+
}),
175+
createTodo: PropTypes.func, // from enhancer (withHandlers)
176+
wasSent: PropTypes.bool, // from enhancer (withStateHandlers)
177+
}
178+
179+
const enhance = compose(
180+
withFirebase,
181+
withStateHandlers(
182+
({ initialWasSent = false }) => ({
183+
wasSent: initialWasSent,
184+
}),
185+
{
186+
toggleSent: ({ wasSent }) => () => ({
187+
wasSent: !wasSent
188+
})
189+
}
190+
}),
191+
withHandlers({
192+
createTodo: ({ wasSent, toggleSent }) => event => {
193+
return props.firebase
194+
.push('todos', { some: 'data' })
195+
.then(() => {
196+
toggleSent()
197+
})
198+
}
199+
})
200+
)
201+
202+
// Export enhanced component
203+
export default enhance(SimpleComponent)
204+
// firebaseConnect can also be used (helpful for creating listeners at the same time)
205+
// export default firebaseConnect()(SimpleComponent)
206+
```
207+
116208
Fun Fact: This is actually what happens internally with both `withFirebase` and `firebaseConnect`.
117209

118210
## Advanced Actions

docs/recipes/profile.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,32 @@ Profile object is used to store data associated with a user. Using profile is in
55
## Basic
66
It is common to store the list of user profiles under a collection called "users" or "profiles". For this example we will use "users".
77

8-
Include the `userProfile` parameter in config when setting up store middleware:
8+
Include the `userProfile` parameter in config when setting up store enhancer:
99

1010
```js
1111
const config = {
1212
userProfile: 'users', // where profiles are stored in database
1313
}
14+
// During store creation
1415
reactReduxFirebase(fbConfig, config)
1516
```
1617

17-
Then later wrap a component with connect:
18+
Then later `connect` (from [react-redux](https://github.com/reactjs/react-redux/blob/master/docs/api.md)) to redux state with:
1819

1920
```js
2021
import { connect } from 'react-redux'
2122

2223
// grab profile from redux with connect
2324
connect(
24-
(state) => ({
25-
profile: state.firebase.profile // profile passed as props.profile
26-
})
25+
(state) => {
26+
return {
27+
profile: state.firebase.profile // profile passed as props.profile
28+
}
29+
}
2730
)(SomeComponent) // pass component to be wrapped
2831

2932
// or with some shorthand:
30-
connect(({ firebase: { profile } }) => ({
31-
profile // profile passed as props.profile
32-
}))(SomeComponent) // pass component to be wrapped
33+
connect(({ firebase: { profile } }) => ({ profile }))(SomeComponent)
3334
```
3435

3536
## Update Profile
@@ -67,10 +68,10 @@ UpdateProfilePage.propTypes = {
6768
}
6869

6970
export default compose(
70-
withFirebase, // firebaseConnect() can also be used
71+
withFirebase, // add props.firebase (firebaseConnect() can also be used)
7172
connect(
7273
({ firebase: { profile } }) => ({
73-
profile,
74+
profile
7475
})
7576
)
7677
)(UpdateProfilePage)

package-lock.json

Lines changed: 1 addition & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-redux-firebase",
3-
"version": "2.0.0-beta.18",
3+
"version": "2.0.0-beta.19",
44
"description": "Redux integration for Firebase. Comes with a Higher Order Components for use with React.",
55
"main": "lib/index.js",
66
"module": "es/index.js",
@@ -13,7 +13,7 @@
1313
"lint:fix": "npm run lint -- --fix",
1414
"test": "mocha -R spec ./tests/setup.js ./tests/** --recursive --require babel-register",
1515
"test:cov": "istanbul cover ./node_modules/mocha/bin/_mocha -- ./tests/** --recursive --report lcov --require babel-register",
16-
"codecov": "cat coverage/*/lcov.info | codecov",
16+
"codecov": "cat coverage/lcov.info | codecov",
1717
"build:commonjs": "cross-env BABEL_ENV=commonjs babel src --out-dir lib",
1818
"build:es": "cross-env BABEL_ENV=es babel src --out-dir es",
1919
"build:umd": "cross-env BABEL_ENV=commonjs NODE_ENV=development webpack src/index.js dist/react-redux-firebase.js",
@@ -32,7 +32,7 @@
3232
"docs:upload": "node bin/api-docs-upload"
3333
},
3434
"license": "MIT",
35-
"homepage": "https://github.com/prescottprue/react-redux-firebase#readme",
35+
"homepage": "http://react-redux-firebase.com",
3636
"repository": {
3737
"type": "git",
3838
"url": "git+https://github.com/prescottprue/react-redux-firebase.git"
@@ -64,9 +64,6 @@
6464
"peerDependencies": {
6565
"react": "^0.14.6 || ^15.0.0-0 || ^16.0.0-0"
6666
},
67-
"optionalDependencies": {
68-
"redux-firestore": "latest"
69-
},
7067
"devDependencies": {
7168
"babel-cli": "^6.24.0",
7269
"babel-core": "^6.24.0",

tests/unit/firestoreConnect.spec.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ describe('firestoreConnect', () => {
105105
})
106106

107107
describe('sets displayName static as ', () => {
108-
describe('FirestoreConnect(${WrappedComponentName}) for', () => {
109-
// eslint-disable-line no-template-curly-in-string
108+
describe('FirestoreConnect(${WrappedComponentName}) for', () => { // eslint-disable-line no-template-curly-in-string
110109
it('standard components', () => {
111110
class TestContainer extends Component {
112111
render () {

0 commit comments

Comments
 (0)