Skip to content

Instantly share code, notes, and snippets.

@Louiefigz
Last active May 23, 2017 14:27
Show Gist options
  • Save Louiefigz/5cf2088e8cb2824c745e35f81ed0942a to your computer and use it in GitHub Desktop.
Save Louiefigz/5cf2088e8cb2824c745e35f81ed0942a to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
// Must use connect() here because we have our App.js wrapped in a Provider.
// Once there's a change in the state, the Provider will re-render this component
// because it is a child of App.js
import { connect } from 'react-redux';
//Below we are exporting the Tasks class so that we can call it in our
//connect(), method on the bottom.
export class Tasks extends Component {
render() {
// this props.tasks would not have existed without using the connect()
// and classifying what tasks would be.
const tasks = this.props.tasks.map((task, index) => <li key={index}>{task}</li>)
return (
<div>
<ul>
{tasks}
</ul>
</div>
)
}
}
// We are now turning the state attributes into props for this component only.
const mapStateToProps = (state) => {
return {
tasks: state.tasks
};
};
// Make sure that the const variable is in uppercase.
// By using connect(), We are making the state information
// available to the Tasks class as props.
// calling this.props would look like the following:
// Object { tasks: Array(0), dispatch: function }
export const AllTasks = connect(mapStateToProps)(Tasks);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment