Skip to content

Commit

Permalink
Generate VisibleTodoList container with connect()
Browse files Browse the repository at this point in the history
  • Loading branch information
zainsra7 committed Jan 30, 2022
1 parent 3e222b2 commit c1bb373
Showing 1 changed file with 24 additions and 40 deletions.
64 changes: 24 additions & 40 deletions src/index.js
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import { useRef } from 'react';
import { combineReducers } from './combinedReducers';
import { createStore } from './createStore';
import { Provider, connect } from 'react-redux';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';

Expand Down Expand Up @@ -161,27 +162,29 @@ class FilterLink extends React.Component {
);
}
}
class VisibileTodoList extends React.Component {
componentDidMount(){
this.unsubscribe = this.context.store.subscribe(() => this.forceUpdate());
}
componentWillUnmount(){
this.unsubscribe();
}
render(){
const {store} = this.context;
const {todos, visibilityFilter} = store.getState();
return(
<TodoList
todos={getVisibleTodos(todos,visibilityFilter)}
onTodoClick={id => store.dispatch({
type: TOGGLE_TODO,
id
})}
/>
);
}
}

const mapStateToProps = (state) => {
return {
todos: getVisibleTodos(
state.todos,
state.visibilityFilter
)
};
};

const mapDispatchToProps = (dispatch) => {
return {
onTodoClick: (id) => {
dispatch({
type: TOGGLE_TODO,
id
})
}
};
};

const VisibileTodoList = connect(mapStateToProps, mapDispatchToProps)(TodoList);

const AddTodo = (props, {store}) => {
const inputEl = useRef(null);
return (
Expand All @@ -203,25 +206,6 @@ const AddTodo = (props, {store}) => {
</div>
);
}

class Provider extends React.Component {
getChildContext(){
return {
store: this.props.store
};
}
render(){
return this.props.children;
}
}

// Types
Provider.childContextTypes = {
store: PropTypes.object
}
VisibileTodoList.contextTypes = {
store: PropTypes.object
}
AddTodo.contextTypes = {
store: PropTypes.object
}
Expand Down

0 comments on commit c1bb373

Please sign in to comment.