Skip to content

Instantly share code, notes, and snippets.

View deep node clone.js
const getStackTo = (node, find, stack = []) => {
if (node === find) {
return stack;
}
if (!node.children) {
return;
}
if (node.children === find) {
return [...stack, 'children'];
View shader.glsl
// ----- Given this shader:
vec4 some_noise_fn() {
return vec4(1.0, 2.0, 3.0, 4.0);
}
void main() {
return some_noise_fn();
}
View hole.glsl
# ----- Given this shader
vec4 some_noise_fn() {
return vec4(1.0, 2.0, 3.0, 4.0);
}
void main() {
return some_noise_fn();
}
View form-consumer.jsx
export class MyUserForm extends Component {
constructor() {
this.state = { myFormData: {} };
}
render() {
const { myFormData } = this.state;
return (
<MagicForm onChange={data => this.setState({ myFormData: data })}>
@AndrewRayCode
AndrewRayCode / async.willmount.test.js
Created Sep 4, 2018
Jest / sinon async componentWillMount test utility function
View async.willmount.test.js
import { spy } from 'sinon';
import { shallow } from 'enzyme';
// Utility method to asynchronously wait for component's willMount function to
// complete. Requires the componentWillMount method *returns* its async promise
// to wait for
export const willMount = async unmountedComponent => {
// Spy on the willmount method
const lifecycleMethod = spy(unmountedComponent.type.prototype, 'componentWillMount');
const wrapper = shallow(unmountedComponent);
View User.graphql
query UserWithAvatarWithoutFriend {
user(id: '1') {
name
avatarImage
}
}
query UserWithoutAvatar {
user(id: '1') {
name
View promise.js
const { baseUrl } = config;
getEndpoints(baseUrl).then(endpoints => {
const data = new Array(endpoints.length);
// Update the array as endpoints come in
endpoints.map((endpoint, index) => {
getEndpointData(endpoint, baseUrl).then(response => {
data[index] = {endpoint, response};
this.setState({ data });
});
View mount.js
class A extends Component {
componentDidMount() {
const { baseUrl } = config;
const data = [];
Promise.all(getEndpoints(baseUrl).then(endpoints => {
return Promise.all(endpoints.map(endpoint => {
return getEndpointData(endpoint, baseUrl).then(response => {
return {endpoint, response};
});
}));
View how-to-test.jsx
// generic ui component
class Panel {
render() {
return <div>
<b>{this.props.title}</b>
<p>{this.props.body}</p>
</div>;
}
}
View status.jsx
const newStatus = { ...this.state.status }; // Copy the object
newStatus['Downloading the song'][1] = 'Info';
this.setState({status : newStatus });
You can’t perform that action at this time.