From 0ae5614b435ad11cac20c425957a016454636f0f Mon Sep 17 00:00:00 2001 From: Sergey Date: Mon, 25 Mar 2019 15:36:45 -0700 Subject: [PATCH] fix bugs and add comments --- test.js | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/test.js b/test.js index a379ef8..80b6562 100644 --- a/test.js +++ b/test.js @@ -13,23 +13,26 @@ const createStore = (reducer, initialState) => { const subscribe = listener => listeners.push(listener) - return { getState, dispatch, subscribe } + return {getState, dispatch, subscribe} } const connect = (mapStateToProps, mapDispatchToProps) => Component => { + // need add window for store (store is global odj) and put props into component return class extends React.Component { render() { return ( ) } componentDidMount() { - store.subscribe(this.handleChange) + // stor is global in Provider + window.store.subscribe(this.handleChange) } handleChange = () => { @@ -61,7 +64,7 @@ const addTodo = todo => ({ // reducers const reducer = (state = [], action) => { - switch(action.type) { + switch (action.type) { case ADD_TODO: state.push(action.payload) return state @@ -88,6 +91,7 @@ class ToDoComponent extends React.Component { /> @@ -95,16 +99,23 @@ class ToDoComponent extends React.Component { ) } - updateText(e) { - const { value } = e.target - - this.state.todoText = value + // updateText has own this and this.state is not avalible + // can create like arrow func without own this + updateText = (e) => { + const value = e.target.value + // for state updating, need use setState() + this.setState({todoText: value}) } - addTodo() { - this.props.addTodo(this.state.todoText) - - this.state.todoText = '' + addTodo = () => { + //check for empty input + if (this.state.todoText !== '') { + this.props.addTodo(this.state.todoText) + // for state updating, need use setState() + this.setState({todoText: ""}) + } else { + console.log('WARNING - an attempt to add an empty value to the TODO list') + } } }