This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const songs = [ | |
{ | |
id: 1, | |
name: "Curl of the Burl", | |
artist: "Mastodon", | |
}, | |
{ | |
id: 2, | |
name: "Oblivion", | |
artist: "Mastodon", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var allSongNames = songs.map(function (song) { | |
return song.name; | |
}); | |
// ES6 | |
const allSongNames = songs.map(song => { | |
return song.name; | |
}); | |
console.log(allSongNames); // ["Curl of the Burl","Oblivion","Flying Whales","L'Enfant Sauvage"]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Mouse extends React.Component { | |
constructor(props) { | |
super(props); | |
this.handleMouseMove = this.handleMouseMove.bind(this); | |
this.state = { x: 0, y: 0 }; | |
} | |
handleMouseMove(event) { | |
this.setState({ | |
x: event.clientX, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@withI18n | |
class MyComponent extends Component { | |
render() { | |
return <div>{this.props.locale}</div> | |
} | |
} | |
// or -- depending on your decorator usage | |
class MyComponent extends Component { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
import PropTypes from "prop-types"; | |
const withI18n = ComponentToWrap => { | |
return class I18nComponent extends Component { | |
static contextTypes = { | |
t: PropTypes.func.isRequired, | |
locale: PropTypes.string.isRequired | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
import PropTypes from "prop-types"; | |
import Polyglot from "node-polyglot"; | |
// for example | |
import translations from "../my-translations.json"; | |
class I18nProvider extends Component { | |
static propTypes = { | |
locale: PropTypes.string.isRequired, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
export default class Avatar extends React.Component { | |
state = { | |
open: false, | |
}; | |
shouldComponentUpdate(nextProps, nextState) { | |
const photoChanged = this.props.avatarUrl !== nextProps.avatarUrl; | |
const optionsWereToggled = this.state.open !== nextState.open; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import { Formik, Form, Field } from "formik"; | |
import yup from "yup"; | |
class Form extends React.Component { | |
state = { error: false }; | |
_submit = async (values, { setSubmitting }) => { | |
// build form data | |
const formData = new FormData(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import PropTypes from "prop-types"; | |
class AjaxForm extends React.Component { | |
static propTypes = { | |
type: PropTypes.string.isRequired, | |
url: PropTypes.string.isRequired, | |
onSuccess: PropTypes.func.isRequired, | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<List | |
url="https://api.github.com/users/JoaoCnh/repos" | |
render={({ list, isLoading }) => ( | |
<div> | |
<h2>My repos</h2> | |
{isLoading && <h2>Loading...</h2>} | |
<ul> | |
{list.length > 0 && list.map(repo => ( | |
<li key={repo.id}> |