Skip to content

Instantly share code, notes, and snippets.

@madetech-com
Created June 22, 2016 10:18
Show Gist options
  • Save madetech-com/4fc710fe4d9a8078f7a73c207c18342c to your computer and use it in GitHub Desktop.
Save madetech-com/4fc710fe4d9a8078f7a73c207c18342c to your computer and use it in GitHub Desktop.
Pros and Cons of React + Redux
{ type: 'ADD_COMMENT', comment: { body: 'test comment', user: 1 } }
import React from 'react'
import style from './style.css'
export default class CTAButton extends React.Component {
render () {
return (
<a href={this.props.href} style={style}>
<h3>{this.props.title}</h3>
<span>{this.props.body}</span>
</a>
)
}
}
<CTAButton title="Shop Now" body="While stocks last!" href="/shop" />
{
tweets: [
{ text: 'Interested in #ContinuousDelivery? Check out our new book!', user: 'madetech_com' },
{ text: 'Join us tomorrow night for #QuizBuzz, a code-themed pub quiz - free to enter, prizes to win, food & drink provided', user: 'madetech_com' },
{ text: 'Overstacked? The journey to becoming a full stack developer: https://t.co/cwEqxPM1sE', user: 'madetech_com' }
]
}
import React from 'react'
import { connect } from 'react-redux'
import store from './store'
import TweetList from '../components/TweetList'
class TweetDash extends React.Component {
render() {
return (
<section>
<h1>Dashboard</h1>
// The 'tweets' section of state is passed to the component as a 'prop'
// You can pass this data down to sub-components
<TweetList tweets={this.props.tweets} />
</section>
)
}
}
function mapStateToProps(state) {
// Redux diffs this against the full state internally to determine
// which parts of the state this component needs. It'll only re-render
// this component when these parts of the state change.
return { tweets: state.tweets }
}
export default connect(mapStateToProps)(TweetDash)
class LikeButton extends React.Component {
render () {
const text = this.props.liked ? 'Unlike' : 'Like'
return (
<button onClick={::this.handleClick}>
{text}
</button>
)
}
handleClick (e) {
e.preventDefault()
// Proxy the onClick event to the component using this component.
// This is quite common when building re-usable components.
this.props.onClick(!this.props.liked)
}
}
<LikeButton liked={true} onClick={::console.log} />
{
"scripts": {
"start": "webpack-dev-server -d --history-api-fallback --hot --inline --progress --colors --port 3000",
"build": "NODE_ENV=production webpack --progress --colors"
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment