Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<Component
{...mapStateToProps(store.getState(), this.props)}
{...mapDispatchToProps(store.dispatch, this.props)}
{...this.props}
{...mapStateToProps(window.store.getState(), this.props)}
{...mapDispatchToProps(window.store.dispatch, this.props)}
/>
)
}

componentDidMount() {
store.subscribe(this.handleChange)
// stor is global in Provider
window.store.subscribe(this.handleChange)
}

handleChange = () => {
Expand Down Expand Up @@ -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
Expand All @@ -88,23 +91,31 @@ class ToDoComponent extends React.Component {
/>
<button onClick={this.addTodo}>Добавить</button>
<ul>
{/*need use key for array*/}
{this.props.todos.map((todo, idx) => <li>{todo}</li>)}
</ul>
</div>
</div>
)
}

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')
}
}
}

Expand Down