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 { usersList } = props
const { isLoading, fetchUsers, userList } = this.props return (
<div>
<Header />
<h1> Github users </h1>
<p>
Server rendered github user list. You can also reload the users from the
api by clicking the <b>Get users</b> button below.
</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}
<p>
<button onClick={fetchUsers}>Get users</button>
</p>
{users.length === 0 ? null : <h1> Users fetched from async function</h1>}
{users.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>
))}
<br />
<CounterDisplay />
</div>
)
}
return ( export async function getStaticProps() {
<div> const store = initializeStore()
<Header /> const usersList = await store.dispatch.github.fetchUsers()
<h1> Github users </h1>
<p>
Server rendered github user list. You can also reload the users from
the api by clicking the <b>Get users</b> button below.
</p>
{isLoading ? <p>Loading ...</p> : null}
<p>
<button onClick={fetchUsers}>Get users</button>
</p>
{userList.map((user) => (
<div key={user.login}>
<Link href={user.html_url} passHref>
<a>
<img height="45" width="45" src={user.avatar_url} />
<span> Username - {user.login}</span>
</a>
</Link>
<br />
</div>
))}
<br />
<CounterDisplay /> return {
</div> props: {
) usersList,
},
} }
} }
const mapState = (state) => ({ export default Github
userList: state.github.users,
isLoading: state.github.isLoading,
})
const mapDispatch = ({ github: { fetchUsers } }) => ({
fetchUsers: () => fetchUsers(),
})
export default connect(mapState, mapDispatch)(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>
<Header /> <Header />
<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}>
</p> incrementAsync
<br /> </button>
</div> </p>
) <br />
} </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 { const CounterDisplay = () => {
render() { const counter = useSelector((state) => state.counter)
const { counter, incrementBy3 } = this.props const { increment } = useRematchDispatch((dispatch) => ({
increment: dispatch.counter.increment,
return ( }))
<div> return (
<h3> Counter </h3> <>
<p> <h3> Counter </h3>
This counter is connected via the <b>connect</b> function. Components <p>
which are not pages can be connected using the connect function just This counter is connected via the <b>useSelector</b> hook. Components
like redux components. which are not pages can be connected using the useSelector hook just
</p> like redux components.
<p>Current value {counter} </p> </p>
<p> <p>Current value {counter}</p>
<button onClick={incrementBy3}>Increment by 3</button> <p>
</p> <button onClick={() => increment(3)}>Increment by 3</button>
</div> </p>
) </>
} )
} }
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> <a href="/">Home</a>
<Link href="/" passHref> </li>
<a>Home</a> <li>
</Link> <a href="/github-users">Async Example </a>
</li> </li>
<li> </ul>
<Link href="/github-users" passHref> </nav>
<a>Async Example </a> </div>
</Link> )
</li>
</ul>
</nav>
</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