Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
53853d3f
Commit
53853d3f
authored
Jul 21, 2018
by
Tomek
Committed by
Tim Neutkens
Jul 21, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update with-redux-observable example (#4818)
parent
822cc3c8
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
62 additions
and
34 deletions
+62
-34
README.md
examples/with-redux-observable/README.md
+5
-4
package.json
examples/with-redux-observable/package.json
+5
-7
_app.js
examples/with-redux-observable/pages/_app.js
+28
-0
index.js
examples/with-redux-observable/pages/index.js
+6
-6
epics.js
examples/with-redux-observable/redux/epics.js
+12
-13
index.js
examples/with-redux-observable/redux/index.js
+6
-4
No files found.
examples/with-redux-observable/README.md
View file @
53853d3f
...
@@ -47,13 +47,14 @@ probably making initial requests on a server. That's because, the
...
@@ -47,13 +47,14 @@ probably making initial requests on a server. That's because, the
`getInitialProps`
hook runs on the server-side before epics have been made available by just dispatching actions.
`getInitialProps`
hook runs on the server-side before epics have been made available by just dispatching actions.
However, we can access and execute epics directly. In order to do so, we need to
However, we can access and execute epics directly. In order to do so, we need to
pass them an Observable of an action and they will return an Observable:
pass them an Observable of an action
together with StateObservable
and they will return an Observable:
```
js
```
js
static
async
getInitialProps
({
store
,
isServer
})
{
static
async
getInitialProps
({
store
,
isServer
})
{
const
state$
=
new
StateObservable
(
new
Subject
(),
store
.
getState
());
const
resultAction
=
await
rootEpic
(
const
resultAction
=
await
rootEpic
(
of
(
actions
.
fetchCharacter
(
isServer
)),
of
(
actions
.
fetchCharacter
(
isServer
)),
st
ore
st
ate$
).
toPromise
();
// we need to convert Observable to Promise
).
toPromise
();
// we need to convert Observable to Promise
store
.
dispatch
(
resultAction
)};
store
.
dispatch
(
resultAction
)};
```
```
...
@@ -61,8 +62,8 @@ static async getInitialProps({ store, isServer }) {
...
@@ -61,8 +62,8 @@ static async getInitialProps({ store, isServer }) {
Note: we are not using
`AjaxObservable`
from the
`rxjs`
library; as of rxjs
Note: we are not using
`AjaxObservable`
from the
`rxjs`
library; as of rxjs
v5.5.6, it will not work on both the server- and client-side. Instead we call
v5.5.6, it will not work on both the server- and client-side. Instead we call
the default export from
the default export from
[
universal-rx
-request
](
https://www.npmjs.com/package/universal-rx-request
)
(
as
[
universal-rx
js-ajax
](
https://www.npmjs.com/package/universal-rxjs-ajax
)
(
as
`
ajax
`
).
`
request
`
).
We transform the Observable we get from
`ajax`
into a Promise in order to await
We transform the Observable we get from
`ajax`
into a Promise in order to await
its resolution. That resolution should be a action (since the epic returns
its resolution. That resolution should be a action (since the epic returns
...
...
examples/with-redux-observable/package.json
View file @
53853d3f
...
@@ -9,17 +9,15 @@
...
@@ -9,17 +9,15 @@
"author"
:
"tomaszmularczyk(tomasz.mularczyk89@gmail.com)"
,
"author"
:
"tomaszmularczyk(tomasz.mularczyk89@gmail.com)"
,
"dependencies"
:
{
"dependencies"
:
{
"next"
:
"latest"
,
"next"
:
"latest"
,
"next-redux-wrapper"
:
"^
1.0.0
"
,
"next-redux-wrapper"
:
"^
2.0.0-beta.6
"
,
"react"
:
"^16.0.0"
,
"react"
:
"^16.0.0"
,
"react-dom"
:
"^16.0.0"
,
"react-dom"
:
"^16.0.0"
,
"react-redux"
:
"^5.0.1"
,
"react-redux"
:
"^5.0.1"
,
"redux"
:
"^
3.6
.0"
,
"redux"
:
"^
4.0
.0"
,
"redux-logger"
:
"^3.0.6"
,
"redux-logger"
:
"^3.0.6"
,
"redux-observable"
:
"^0.17.0"
,
"redux-observable"
:
"^1.0.0"
,
"redux-thunk"
:
"^2.1.0"
,
"rxjs"
:
"^6.2.1"
,
"rxjs"
:
"^5.5.2"
,
"universal-rxjs-ajax"
:
"^2.0.0"
"superagent"
:
"^3.8.1"
,
"universal-rx-request"
:
"^1.0.3"
},
},
"license"
:
"ISC"
"license"
:
"ISC"
}
}
examples/with-redux-observable/pages/_app.js
0 → 100644
View file @
53853d3f
import
React
from
'react'
import
{
Provider
}
from
'react-redux'
import
App
,
{
Container
}
from
'next/app'
import
withRedux
from
'next-redux-wrapper'
import
makeStore
from
'../redux'
class
MyApp
extends
App
{
static
async
getInitialProps
({
Component
,
ctx
})
{
const
pageProps
=
Component
.
getInitialProps
?
await
Component
.
getInitialProps
(
ctx
)
:
{}
return
{
pageProps
}
}
render
()
{
const
{
Component
,
pageProps
,
store
}
=
this
.
props
return
(
<
Container
>
<
Provider
store
=
{
store
}
>
<
Component
{...
pageProps
}
/
>
<
/Provider
>
<
/Container
>
)
}
}
export
default
withRedux
(
makeStore
)(
MyApp
)
examples/with-redux-observable/pages/index.js
View file @
53853d3f
import
React
from
'react'
import
React
from
'react'
import
Link
from
'next/link'
import
Link
from
'next/link'
import
withRedux
from
'next-redux-wrapper'
import
{
of
,
Subject
}
from
'rxjs'
import
initStore
from
'../redux'
import
{
StateObservable
}
from
'redux-observable'
import
{
connect
}
from
'react-redux'
import
CharacterInfo
from
'../components/CharacterInfo'
import
CharacterInfo
from
'../components/CharacterInfo'
import
{
rootEpic
}
from
'../redux/epics'
import
{
rootEpic
}
from
'../redux/epics'
import
*
as
actions
from
'../redux/actions'
import
*
as
actions
from
'../redux/actions'
import
{
of
}
from
'rxjs/observable/of'
class
Counter
extends
React
.
Component
{
class
Counter
extends
React
.
Component
{
static
async
getInitialProps
({
store
,
isServer
})
{
static
async
getInitialProps
({
store
,
isServer
})
{
const
state$
=
new
StateObservable
(
new
Subject
(),
store
.
getState
())
const
resultAction
=
await
rootEpic
(
const
resultAction
=
await
rootEpic
(
of
(
actions
.
fetchCharacter
(
isServer
)),
of
(
actions
.
fetchCharacter
(
isServer
)),
st
ore
st
ate$
).
toPromise
()
// we need to convert Observable to Promise
).
toPromise
()
// we need to convert Observable to Promise
store
.
dispatch
(
resultAction
)
store
.
dispatch
(
resultAction
)
...
@@ -40,8 +41,7 @@ class Counter extends React.Component {
...
@@ -40,8 +41,7 @@ class Counter extends React.Component {
}
}
}
}
export
default
withRedux
(
export
default
connect
(
initStore
,
null
,
null
,
{
{
startFetchingCharacters
:
actions
.
startFetchingCharacters
,
startFetchingCharacters
:
actions
.
startFetchingCharacters
,
...
...
examples/with-redux-observable/redux/epics.js
View file @
53853d3f
import
{
interval
}
from
'rxjs/observable/interval'
import
{
interval
,
of
}
from
'rxjs'
import
{
of
}
from
'rxjs/observable/of'
import
{
takeUntil
,
mergeMap
,
catchError
,
map
}
from
'rxjs/operators'
import
{
takeUntil
,
mergeMap
,
catchError
,
map
}
from
'rxjs/operators'
import
{
combineEpics
,
ofType
}
from
'redux-observable'
import
{
combineEpics
,
ofType
}
from
'redux-observable'
import
ajax
from
'universal-rx-request
'
// because standard AjaxObservable only works in browser
import
{
request
}
from
'universal-rxjs-ajax
'
// because standard AjaxObservable only works in browser
import
*
as
actions
from
'./actions'
import
*
as
actions
from
'./actions'
import
*
as
types
from
'./actionTypes'
import
*
as
types
from
'./actionTypes'
export
const
fetchUserEpic
=
(
action$
,
st
ore
)
=>
export
const
fetchUserEpic
=
(
action$
,
st
ate$
)
=>
action$
.
pipe
(
action$
.
pipe
(
ofType
(
types
.
START_FETCHING_CHARACTERS
),
ofType
(
types
.
START_FETCHING_CHARACTERS
),
mergeMap
(
action
=>
{
mergeMap
(
action
=>
{
return
interval
(
3000
).
pipe
(
return
interval
(
3000
).
pipe
(
m
ergeM
ap
(
x
=>
map
(
x
=>
actions
.
fetchCharacter
({
actions
.
fetchCharacter
({
isServer
:
st
ore
.
getState
()
.
isServer
isServer
:
st
ate$
.
value
.
isServer
})
})
),
),
takeUntil
(
action$
.
ofType
(
types
.
STOP_FETCHING_CHARACTERS
))
takeUntil
(
action$
.
ofType
(
types
.
STOP_FETCHING_CHARACTERS
))
...
@@ -22,24 +21,24 @@ export const fetchUserEpic = (action$, store) =>
...
@@ -22,24 +21,24 @@ export const fetchUserEpic = (action$, store) =>
})
})
)
)
export
const
fetchCharacterEpic
=
(
action$
,
st
ore
)
=>
export
const
fetchCharacterEpic
=
(
action$
,
st
ate$
)
=>
action$
.
pipe
(
action$
.
pipe
(
ofType
(
types
.
FETCH_CHARACTER
),
ofType
(
types
.
FETCH_CHARACTER
),
mergeMap
(
action
=>
mergeMap
(
action
=>
ajax
({
request
({
url
:
`https://swapi.co/api/people/
${
st
ore
.
getState
()
.
nextCharacterId
}
`
url
:
`https://swapi.co/api/people/
${
st
ate$
.
value
.
nextCharacterId
}
`
}).
pipe
(
}).
pipe
(
map
(
response
=>
map
(
response
=>
actions
.
fetchCharacterSuccess
(
actions
.
fetchCharacterSuccess
(
response
.
body
,
response
.
response
,
st
ore
.
getState
()
.
isServer
st
ate$
.
value
.
isServer
)
)
),
),
catchError
(
error
=>
catchError
(
error
=>
of
(
of
(
actions
.
fetchCharacterFailure
(
actions
.
fetchCharacterFailure
(
error
.
response
.
body
,
error
.
xhr
.
response
,
st
ore
.
getState
()
.
isServer
st
ate$
.
value
.
isServer
)
)
)
)
)
)
...
...
examples/with-redux-observable/redux/index.js
View file @
53853d3f
import
{
createStore
,
applyMiddleware
}
from
'redux'
import
{
createStore
,
applyMiddleware
}
from
'redux'
import
thunkMiddleware
from
'redux-thunk'
import
{
createLogger
}
from
'redux-logger'
import
{
createLogger
}
from
'redux-logger'
import
{
createEpicMiddleware
}
from
'redux-observable'
import
{
createEpicMiddleware
}
from
'redux-observable'
import
reducer
from
'./reducer'
import
reducer
from
'./reducer'
import
{
rootEpic
}
from
'./epics'
import
{
rootEpic
}
from
'./epics'
export
default
function
initStore
(
initialState
)
{
export
default
function
initStore
(
initialState
)
{
const
epicMiddleware
=
createEpicMiddleware
(
rootEpic
)
const
epicMiddleware
=
createEpicMiddleware
()
const
logger
=
createLogger
({
collapsed
:
true
})
// log every action to see what's happening behind the scenes.
const
logger
=
createLogger
({
collapsed
:
true
})
// log every action to see what's happening behind the scenes.
const
reduxMiddleware
=
applyMiddleware
(
thunkMiddleware
,
epicMiddleware
,
logger
)
const
reduxMiddleware
=
applyMiddleware
(
epicMiddleware
,
logger
)
return
createStore
(
reducer
,
initialState
,
reduxMiddleware
)
const
store
=
createStore
(
reducer
,
initialState
,
reduxMiddleware
)
epicMiddleware
.
run
(
rootEpic
)
return
store
};
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment