-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use proxyequal to check changes (ref. #1)
- Loading branch information
Showing
10 changed files
with
218 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import * as React from 'react'; | ||
import { createStore } from 'redux'; | ||
|
||
import { ReduxProvider } from '../../src/index'; | ||
|
||
import { reducer } from './state'; | ||
|
||
import Counter from './Counter'; | ||
import Person from './Person'; | ||
|
||
const store = createStore(reducer); | ||
|
||
const App = () => ( | ||
<ReduxProvider store={store}> | ||
<h1>Counter</h1> | ||
<Counter /> | ||
<Counter /> | ||
<h1>Person</h1> | ||
<Person /> | ||
<Person /> | ||
</ReduxProvider> | ||
); | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import * as React from 'react'; | ||
|
||
import { bailOutHack, useReduxDispatch, useReduxState } from '../../src/index'; | ||
|
||
import { Action, State } from './state'; | ||
|
||
const Counter = bailOutHack(() => { | ||
const state = useReduxState<State>([]); | ||
const dispatch = useReduxDispatch<Action>(); | ||
return ( | ||
<div> | ||
{Math.random()} | ||
<div> | ||
<span> | ||
Count: | ||
{state.counter} | ||
</span> | ||
<button type="button" onClick={() => dispatch({ type: 'increment' })}>+1</button> | ||
<button type="button" onClick={() => dispatch({ type: 'decrement' })}>-1</button> | ||
</div> | ||
</div> | ||
); | ||
}); | ||
|
||
export default Counter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import * as React from 'react'; | ||
|
||
import { bailOutHack, useReduxDispatch, useReduxState } from '../../src/index'; | ||
|
||
import { Action, State } from './state'; | ||
|
||
const TextBox: React.SFC<{ text: string }> = ({ text }) => { | ||
// tslint:disable-next-line:no-console | ||
console.log('rendering text:', text); | ||
return <span>{text}</span>; | ||
}; | ||
|
||
const PersonFirstName = bailOutHack(() => { | ||
const state = useReduxState<State>([]); | ||
const dispatch = useReduxDispatch<Action>(); | ||
return ( | ||
<div> | ||
First Name: | ||
<TextBox text={state.person.firstName} /> | ||
<input | ||
value={state.person.firstName} | ||
onChange={(event) => { | ||
const firstName = event.target.value; | ||
dispatch({ firstName, type: 'setFirstName' }); | ||
}} | ||
/> | ||
</div> | ||
); | ||
}); | ||
|
||
const PersonLastName = bailOutHack(() => { | ||
const state = useReduxState<State>([]); | ||
const dispatch = useReduxDispatch<Action>(); | ||
return ( | ||
<div> | ||
Last Name: | ||
<TextBox text={state.person.lastName} /> | ||
<input | ||
value={state.person.lastName} | ||
onChange={(event) => { | ||
const lastName = event.target.value; | ||
dispatch({ lastName, type: 'setLastName' }); | ||
}} | ||
/> | ||
</div> | ||
); | ||
}); | ||
|
||
const PersonAge = bailOutHack(() => { | ||
const state = useReduxState<State>([]); | ||
const dispatch = useReduxDispatch<Action>(); | ||
return ( | ||
<div> | ||
Age: | ||
<input | ||
value={state.person.age} | ||
onChange={(event) => { | ||
const age = Number(event.target.value) || 0; | ||
dispatch({ age, type: 'setAge' }); | ||
}} | ||
/> | ||
</div> | ||
); | ||
}); | ||
|
||
const Person = () => ( | ||
<> | ||
<PersonFirstName /> | ||
<PersonLastName /> | ||
<PersonAge /> | ||
</> | ||
); | ||
|
||
export default Person; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<html> | ||
<head> | ||
<title>react-hooks-easy-redux example</title> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
</body> | ||
<script src="bundle.js"></script> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import * as React from 'react'; | ||
import { render } from 'react-dom'; | ||
|
||
import App from './App'; | ||
|
||
render(React.createElement(App), document.getElementById('app')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const initialState = { | ||
counter: 0, | ||
person: { | ||
age: 0, | ||
firstName: '', | ||
lastName: '', | ||
}, | ||
}; | ||
|
||
export type State = typeof initialState; | ||
|
||
export type Action = | ||
| { type: 'increment' } | ||
| { type: 'decrement' } | ||
| { type: 'setFirstName', firstName: string } | ||
| { type: 'setLastName', lastName: string } | ||
| { type: 'setAge', age: number }; | ||
|
||
export const reducer = (state = initialState, action: Action) => { | ||
switch (action.type) { | ||
case 'increment': return { | ||
...state, | ||
counter: state.counter + 1, | ||
}; | ||
case 'decrement': return { | ||
...state, | ||
counter: state.counter - 1, | ||
}; | ||
case 'setFirstName': return { | ||
...state, | ||
person: { | ||
...state.person, | ||
firstName: action.firstName, | ||
}, | ||
}; | ||
case 'setLastName': return { | ||
...state, | ||
person: { | ||
...state.person, | ||
lastName: action.lastName, | ||
}, | ||
}; | ||
case 'setAge': return { | ||
...state, | ||
person: { | ||
...state.person, | ||
age: action.age, | ||
}, | ||
}; | ||
default: return state; | ||
} | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters