Skip to content

Instantly share code, notes, and snippets.

View MicroBenz's full-sized avatar

Tananan Tangthanachaikul MicroBenz

View GitHub Profile
@MicroBenz
MicroBenz / context-button.js
Created March 10, 2018 14:08
React Context
class Button extends React.Component {
render() {
return (
<button style={{background: this.context.color}}>
{this.props.children}
</button>
);
}
}
class MessageList extends React.Component {
getChildContext() {
// Declare context object
return {color: "purple"};
}
render() {
const children = this.props.messages.map((message) =>
<Message text={message.text} />
);
@MicroBenz
MicroBenz / using-props.js
Created March 10, 2018 13:57
React Context
class Button extends React.Component {
render() {
return (
<button style={{background: this.props.color}}>
{this.props.children}
</button>
);
}
}
@MicroBenz
MicroBenz / Image.js
Created March 10, 2018 13:45
React Context
import React from 'react';
import PropTypes from 'prop-types';
const Image = (props, context) => (
<img src={context.path(props.image, 'thumbnails')} />
);
Image.propTypes = {
image: PropTypes.string
};
@MicroBenz
MicroBenz / UIKitContext.js
Created March 10, 2018 13:39
React Context
import React, { Component } from 'react';
import PropTypes from 'prop-types';
class UIKitContext extends Component {
getChildContext() {
return {
path: (image, type) => `${this.props.prefixPath}/${type}/${image}`
};
}
@MicroBenz
MicroBenz / media.js
Created March 10, 2018 13:36
React Context
const isProduction = process.env.NODE_ENV === 'production';
const mediaPath = isProduction ? 'cloudfront-production-path' : 'cloudfront-development-path';
export const path = (image, type) => `${mediaPath}/${type}/${image}`;
@MicroBenz
MicroBenz / simple-props.js
Created March 10, 2018 13:31
React Context
const Parent = props => (
<div>
<h1>Here is parent</h1>
<Child title="From Parent" />
</div>
);
const Child = ({ title }) => (
<p>Here is child {title}</p>
);
@MicroBenz
MicroBenz / radio-group.js
Created January 3, 2018 09:04
Krit GGWP
class ParentComp extends React.Component {
constructor(props) {
super(props);
this.state = { choice: [] };
}
render() {
return (
<RadioGroup
onChange={value => this.setState({ choice: value })}
value={this.state.choice}
@MicroBenz
MicroBenz / basic-props.js
Created January 2, 2018 05:23
IntroToReact
// ParentComponent.js
export default class ParentComponent extends React.Component {
render() {
return (
<div>
<ChildComponent number={1} />
<ChildComponent number={2} />
<ChildComponent number={3} />
<ChildComponent number={4} />
</div>
@MicroBenz
MicroBenz / basic-component.js
Created January 2, 2018 05:15
IntroToReact
import React from 'react';
export default class BasicComponent extends React.Component {
render() {
return (
<div>
<h1>Hello React</h1>
</div>
);
}