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

Refactor with rematch example (#13392)

parent 658e0f8d
......@@ -7,12 +7,12 @@
"start": "next start"
},
"dependencies": {
"@rematch/core": "^1.1.0",
"@rematch/core": "1.4.0",
"next": "latest",
"react": "^16.8.4",
"react-dom": "^16.8.4",
"react-redux": "^6.0.1",
"redux": "^4.0.1"
"react": "16.13.1",
"react-dom": "16.13.1",
"react-redux": "7.2.0",
"redux": "4.0.5"
},
"license": "ISC"
}
import App from 'next/app'
import React from 'react'
import { Provider } from 'react-redux'
import withRematch from '../shared/withRematch'
import { Provider } from 'react-redux'
class MyApp extends App {
render() {
......
import { Component } from 'react'
import Link from 'next/link'
import { connect } from 'react-redux'
import { checkServer } from '../shared/utils'
import { useSelector } from 'react-redux'
import { useRematchDispatch } from '../shared/utils'
import { initializeStore } from '../shared/store'
import CounterDisplay from '../shared/components/counter-display'
import Header from '../shared/components/header'
class Github extends Component {
static async getInitialProps(ctx) {
const store = ctx.reduxStore
// Pre-populate users only on the server-side
if (checkServer()) {
await store.dispatch.github.fetchUsers()
}
return {}
}
const Github = (props) => {
const github = useSelector((state) => state.github)
const { users, isLoading } = github
const { fetchUsers } = useRematchDispatch((dispatch) => ({
fetchUsers: dispatch.github.fetchUsers,
}))
render() {
const { isLoading, fetchUsers, userList } = this.props
const { usersList } = 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 (
<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>
{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 />
export async function getStaticProps() {
const store = initializeStore()
const usersList = await store.dispatch.github.fetchUsers()
<CounterDisplay />
</div>
)
return {
props: {
usersList,
},
}
}
const mapState = (state) => ({
userList: state.github.users,
isLoading: state.github.isLoading,
})
const mapDispatch = ({ github: { fetchUsers } }) => ({
fetchUsers: () => fetchUsers(),
})
export default connect(mapState, mapDispatch)(Github)
export default Github
import { Component } from 'react'
import { connect } from 'react-redux'
import { useSelector, useDispatch } from 'react-redux'
import Header from '../shared/components/header'
import { useRematchDispatch } from '../shared/utils'
class Home extends Component {
render() {
const { counter, increment, incrementBy, incrementAsync } = this.props
const Home = () => {
const counter = useSelector((state) => state.counter)
const dispatch = useDispatch()
const { increment } = useRematchDispatch((dispatch) => ({
increment: dispatch.counter.increment,
}))
return (
<div>
<Header />
<h1> Counter </h1>
<h3>The count is {counter}</h3>
<p>
<button onClick={increment}>increment</button>
<button onClick={() => increment(1)}>
increment (using dispatch function)
</button>
<button onClick={incrementBy(5)}>increment by 5</button>
<button onClick={incrementAsync}>incrementAsync</button>
</p>
<br />
</div>
)
}
return (
<div>
<Header />
<h1> Counter </h1>
<h3>The count is {counter}</h3>
<p>
<button onClick={dispatch.counter.increment}>increment</button>
<button onClick={() => increment(1)}>
increment (using dispatch function)
</button>
<button onClick={() => increment(5)}>increment by 5</button>
<button onClick={dispatch.counter.incrementAsync}>
incrementAsync
</button>
</p>
<br />
</div>
)
}
const mapState = (state) => ({
counter: state.counter,
})
const mapDispatch = ({ counter: { increment, incrementAsync } }) => ({
increment: () => increment(1),
incrementBy: (amount) => () => increment(amount),
incrementAsync: () => incrementAsync(1),
})
export default connect(mapState, mapDispatch)(Home)
export default Home
import React, { Component } from 'react'
import { connect } from 'react-redux'
import React from 'react'
import { useSelector } from 'react-redux'
import { useRematchDispatch } from '../utils'
class CounterDisplay extends Component {
render() {
const { counter, incrementBy3 } = this.props
return (
<div>
<h3> Counter </h3>
<p>
This counter is connected via the <b>connect</b> function. Components
which are not pages can be connected using the connect function just
like redux components.
</p>
<p>Current value {counter} </p>
<p>
<button onClick={incrementBy3}>Increment by 3</button>
</p>
</div>
)
}
const CounterDisplay = () => {
const counter = useSelector((state) => state.counter)
const { increment } = useRematchDispatch((dispatch) => ({
increment: dispatch.counter.increment,
}))
return (
<>
<h3> Counter </h3>
<p>
This counter is connected via the <b>useSelector</b> hook. Components
which are not pages can be connected using the useSelector hook just
like redux components.
</p>
<p>Current value {counter}</p>
<p>
<button onClick={() => increment(3)}>Increment by 3</button>
</p>
</>
)
}
const mapState = (state) => ({
counter: state.counter,
})
const mapDispatch = ({ counter: { increment, incrementAsync } }) => ({
incrementBy3: () => increment(3),
})
export default connect(mapState, mapDispatch)(CounterDisplay)
export default CounterDisplay
import React, { Component } from 'react'
import Link from 'next/link'
import React from 'react'
class Header extends Component {
render() {
return (
<div>
<nav>
<ul>
<li>
<Link href="/" passHref>
<a>Home</a>
</Link>
</li>
<li>
<Link href="/github-users" passHref>
<a>Async Example </a>
</Link>
</li>
</ul>
</nav>
</div>
)
}
const Header = () => {
return (
<div>
<nav>
<ul>
<li>
<a href="/">Home</a>
</li>
<li>
<a href="/github-users">Async Example </a>
</li>
</ul>
</nav>
</div>
)
}
export default Header
const counter = {
import { createModel } from '@rematch/core'
const counter = createModel({
state: 0, // initial state
reducers: {
// handle state changes with pure functions
increment(state, payload) {
increment: (state, payload) => {
if (typeof payload === 'object') return state + 1
return state + payload
},
},
......@@ -14,6 +17,6 @@ const counter = {
this.increment(payload)
},
},
}
})
export default counter
......@@ -26,6 +26,7 @@ const github = {
const response = await fetch('https://api.github.com/users')
const users = await response.json()
this.receiveUsers(users)
return users
} catch (err) {
console.log(err)
this.receiveUsers([])
......
// Robust way to check if it's Node or browser
import { useDispatch } from 'react-redux'
export const checkServer = () => {
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