Unverified Commit 7ec57c6c authored by TodorTotev's avatar TodorTotev Committed by GitHub

Refactor with rematch example (#13392)

parent 658e0f8d
...@@ -7,12 +7,12 @@ ...@@ -7,12 +7,12 @@
"start": "next start" "start": "next start"
}, },
"dependencies": { "dependencies": {
"@rematch/core": "^1.1.0", "@rematch/core": "1.4.0",
"next": "latest", "next": "latest",
"react": "^16.8.4", "react": "16.13.1",
"react-dom": "^16.8.4", "react-dom": "16.13.1",
"react-redux": "^6.0.1", "react-redux": "7.2.0",
"redux": "^4.0.1" "redux": "4.0.5"
}, },
"license": "ISC" "license": "ISC"
} }
import App from 'next/app' import App from 'next/app'
import React from 'react' import { Provider } from 'react-redux'
import withRematch from '../shared/withRematch' import withRematch from '../shared/withRematch'
import { Provider } from 'react-redux'
class MyApp extends App { class MyApp extends App {
render() { render() {
......
import { Component } from 'react' import { useSelector } from 'react-redux'
import Link from 'next/link' import { useRematchDispatch } from '../shared/utils'
import { connect } from 'react-redux' import { initializeStore } from '../shared/store'
import { checkServer } from '../shared/utils'
import CounterDisplay from '../shared/components/counter-display' import CounterDisplay from '../shared/components/counter-display'
import Header from '../shared/components/header' import Header from '../shared/components/header'
class Github extends Component { const Github = (props) => {
static async getInitialProps(ctx) { const github = useSelector((state) => state.github)
const store = ctx.reduxStore const { users, isLoading } = github
const { fetchUsers } = useRematchDispatch((dispatch) => ({
// Pre-populate users only on the server-side fetchUsers: dispatch.github.fetchUsers,
if (checkServer()) { }))
await store.dispatch.github.fetchUsers()
}
return {}
}
render() {
const { isLoading, fetchUsers, userList } = this.props
const { usersList } = props
return ( return (
<div> <div>
<Header /> <Header />
<h1> Github users </h1> <h1> Github users </h1>
<p> <p>
Server rendered github user list. You can also reload the users from Server rendered github user list. You can also reload the users from the
the api by clicking the <b>Get users</b> button below. api by clicking the <b>Get users</b> button below.
</p> </p>
<h1> Users passed as property from getStaticProps</h1>
{usersList.map((user) => (
<div key={user.login}>
<a href={user.html_url}>
<img height="45" width="45" src={user.avatar_url} />
<span> Username - {user.login}</span>
</a>
<br />
</div>
))}
{isLoading ? <p>Loading ...</p> : null} {isLoading ? <p>Loading ...</p> : null}
<p> <p>
<button onClick={fetchUsers}>Get users</button> <button onClick={fetchUsers}>Get users</button>
</p> </p>
{userList.map((user) => ( {users.length === 0 ? null : <h1> Users fetched from async function</h1>}
{users.map((user) => (
<div key={user.login}> <div key={user.login}>
<Link href={user.html_url} passHref> <a href={user.html_url}>
<a>
<img height="45" width="45" src={user.avatar_url} /> <img height="45" width="45" src={user.avatar_url} />
<span> Username - {user.login}</span> <span> Username - {user.login}</span>
</a> </a>
</Link>
<br /> <br />
</div> </div>
))} ))}
<br /> <br />
<CounterDisplay /> <CounterDisplay />
</div> </div>
) )
}
} }
const mapState = (state) => ({ export async function getStaticProps() {
userList: state.github.users, const store = initializeStore()
isLoading: state.github.isLoading, const usersList = await store.dispatch.github.fetchUsers()
})
const mapDispatch = ({ github: { fetchUsers } }) => ({ return {
fetchUsers: () => fetchUsers(), props: {
}) usersList,
},
}
}
export default connect(mapState, mapDispatch)(Github) export default Github
import { Component } from 'react' import { useSelector, useDispatch } from 'react-redux'
import { connect } from 'react-redux'
import Header from '../shared/components/header' import Header from '../shared/components/header'
import { useRematchDispatch } from '../shared/utils'
class Home extends Component { const Home = () => {
render() { const counter = useSelector((state) => state.counter)
const { counter, increment, incrementBy, incrementAsync } = this.props const dispatch = useDispatch()
const { increment } = useRematchDispatch((dispatch) => ({
increment: dispatch.counter.increment,
}))
return ( return (
<div> <div>
...@@ -13,27 +15,18 @@ class Home extends Component { ...@@ -13,27 +15,18 @@ class Home extends Component {
<h1> Counter </h1> <h1> Counter </h1>
<h3>The count is {counter}</h3> <h3>The count is {counter}</h3>
<p> <p>
<button onClick={increment}>increment</button> <button onClick={dispatch.counter.increment}>increment</button>
<button onClick={() => increment(1)}> <button onClick={() => increment(1)}>
increment (using dispatch function) increment (using dispatch function)
</button> </button>
<button onClick={incrementBy(5)}>increment by 5</button> <button onClick={() => increment(5)}>increment by 5</button>
<button onClick={incrementAsync}>incrementAsync</button> <button onClick={dispatch.counter.incrementAsync}>
incrementAsync
</button>
</p> </p>
<br /> <br />
</div> </div>
) )
}
} }
const mapState = (state) => ({ export default Home
counter: state.counter,
})
const mapDispatch = ({ counter: { increment, incrementAsync } }) => ({
increment: () => increment(1),
incrementBy: (amount) => () => increment(amount),
incrementAsync: () => incrementAsync(1),
})
export default connect(mapState, mapDispatch)(Home)
import React, { Component } from 'react' import React from 'react'
import { connect } from 'react-redux' import { useSelector } from 'react-redux'
import { useRematchDispatch } from '../utils'
class CounterDisplay extends Component {
render() {
const { counter, incrementBy3 } = this.props
const CounterDisplay = () => {
const counter = useSelector((state) => state.counter)
const { increment } = useRematchDispatch((dispatch) => ({
increment: dispatch.counter.increment,
}))
return ( return (
<div> <>
<h3> Counter </h3> <h3> Counter </h3>
<p> <p>
This counter is connected via the <b>connect</b> function. Components This counter is connected via the <b>useSelector</b> hook. Components
which are not pages can be connected using the connect function just which are not pages can be connected using the useSelector hook just
like redux components. like redux components.
</p> </p>
<p>Current value {counter} </p> <p>Current value {counter}</p>
<p> <p>
<button onClick={incrementBy3}>Increment by 3</button> <button onClick={() => increment(3)}>Increment by 3</button>
</p> </p>
</div> </>
) )
}
} }
const mapState = (state) => ({ export default CounterDisplay
counter: state.counter,
})
const mapDispatch = ({ counter: { increment, incrementAsync } }) => ({
incrementBy3: () => increment(3),
})
export default connect(mapState, mapDispatch)(CounterDisplay)
import React, { Component } from 'react' import React from 'react'
import Link from 'next/link'
class Header extends Component { const Header = () => {
render() {
return ( return (
<div> <div>
<nav> <nav>
<ul> <ul>
<li> <li>
<Link href="/" passHref> <a href="/">Home</a>
<a>Home</a>
</Link>
</li> </li>
<li> <li>
<Link href="/github-users" passHref> <a href="/github-users">Async Example </a>
<a>Async Example </a>
</Link>
</li> </li>
</ul> </ul>
</nav> </nav>
</div> </div>
) )
}
} }
export default Header export default Header
const counter = { import { createModel } from '@rematch/core'
const counter = createModel({
state: 0, // initial state state: 0, // initial state
reducers: { reducers: {
// handle state changes with pure functions // handle state changes with pure functions
increment(state, payload) { increment: (state, payload) => {
if (typeof payload === 'object') return state + 1
return state + payload return state + payload
}, },
}, },
...@@ -14,6 +17,6 @@ const counter = { ...@@ -14,6 +17,6 @@ const counter = {
this.increment(payload) this.increment(payload)
}, },
}, },
} })
export default counter export default counter
...@@ -26,6 +26,7 @@ const github = { ...@@ -26,6 +26,7 @@ const github = {
const response = await fetch('https://api.github.com/users') const response = await fetch('https://api.github.com/users')
const users = await response.json() const users = await response.json()
this.receiveUsers(users) this.receiveUsers(users)
return users
} catch (err) { } catch (err) {
console.log(err) console.log(err)
this.receiveUsers([]) this.receiveUsers([])
......
// Robust way to check if it's Node or browser // Robust way to check if it's Node or browser
import { useDispatch } from 'react-redux'
export const checkServer = () => { export const checkServer = () => {
return typeof window === 'undefined' return typeof window === 'undefined'
} }
export const useRematchDispatch = (selector) => {
const dispatch = useDispatch()
return selector(dispatch)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment