Skip to content

Instantly share code, notes, and snippets.

View cecigarcia's full-sized avatar

Cecilia García cecigarcia

  • Trabe Soluciones S.L.
  • España
View GitHub Profile
import React, { Component } from "react";
class MyComponent extends Component {
state = {
data: [],
error: null,
};
handleOnClick = () =>
fetchData()
export const cancellablePromise = promise => {
let isCanceled = false;
const wrappedPromise = new Promise((resolve, reject) => {
promise.then(
value => (isCanceled ? reject({ isCanceled, value }) : resolve(value)),
error => reject({ isCanceled, error }),
);
});
import React, { Component } from "react";
import cancelablePromise from "./cancelable-promise";
class MyComponent extends Component {
state = {
data: [],
error: null,
};
pendingPromises = [];
import React, { Component } from "react";
class CustomComponent extends Component {
apiMethod() { /* ... */ }
}
class ParentComponent extends Component {
handleClick = () => this.component.apiMethod();
render() {
import React, { Component } from "react";
class CustomComponent extends Component {
apiMethod() { /* ... */ }
};
const StyledCustomComponent = props => (
<CustomComponent {...props} style={{ padding: 10 }} />
);
import React, { Component } from "react";
class CustomComponent extends Component {
apiMethod() { /* ... */ }
}
const StyledCustomComponent = props => (
<CustomComponent
ref={props.componentRef}
{...props}
import React, { Component } from "react";
class CustomComponent extends Component {
apiMethod() { /* ... */ }
}
class StyledCustomComponent extends Component {
apiMethod() {
this.wrappedComponent.apiMethod();
}
import React, { Component } from "react";
class CustomComponent extends Component {
constructor(props) {
super(props);
this.innerRef = props.innerRef || (() => null);
}
componentDidMount = () => {
this.innerRef(this);
import React, {Component} from "react";
import translations from "./translations";
class LocaleProvider extends Component {
state = { locale: "es" };
// Defines the Context that React should
// pass down to any component in the subtree
getChildContext() {
return {
import React, {Component} from "react";
// Create the LocaleContext with a default value
const LocaleContext = React.createContext({ locale: "en", changeLocale: () => null });
class LocaleProvider extends Component {
state = { locale: "es" };
render() {
const value = {