Skip to content

Commit 8f5668f

Browse files
committedDec 23, 2019
debug with console.debug or redux-devtools
1 parent 3eb3b17 commit 8f5668f

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed
 

‎src/components/Form/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const Form = () => {
1313
function _onSubmit(e) {
1414
e.preventDefault();
1515
const form = e.target;
16-
dispatch(addTodo({ id: Date.now(), title: form.title.value }));
16+
dispatch(addTodo({ id: Date.now(), name: form.name.value }));
1717
form.reset();
1818
}
1919

@@ -23,7 +23,7 @@ export const Form = () => {
2323

2424
return (
2525
<form onSubmit={_onSubmit}>
26-
<Input required name="title" />
26+
<Input required name="name" />
2727
<Button type="button" onClick={_clear}>Clear</Button>
2828
</form>
2929

‎src/components/List/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const List = () => {
1313

1414
const _item = (todo) => (
1515
<ListItem key={todo.id} className="List-item">
16-
{todo.name} ({todo.species})
16+
{todo.name}
1717
<Button onClick={() => dispatch(removeTodo(todo.id))}>x</Button>
1818
</ListItem>
1919
);

‎src/store/debug.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
const withDevTools = typeof window !== 'undefined' && window.__REDUX_DEVTOOLS_EXTENSION__;
3+
4+
const devTools = withDevTools && window.__REDUX_DEVTOOLS_EXTENSION__.connect();
5+
6+
const debug = (type, state) => {
7+
if (withDevTools) {
8+
devTools.send(type, state);
9+
return;
10+
}
11+
12+
console.debug('store', state);
13+
}
14+
15+
export { debug };

‎src/store/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import React, { createContext, useContext, useReducer, useCallback } from 'react';
22

3+
import { debug } from './debug';
4+
35
import { initialState as todosState } from './todos/initialState';
46
import { reducer as todosReducer } from './todos/reducer';
57

@@ -14,6 +16,7 @@ const reducers = {
1416
function reducer(state, action) {
1517
const { type } = action;
1618
const newState = !reducers[type] ? state : reducers[type](state, action);
19+
debug(type, newState);
1720
return newState;
1821
}
1922