Skip to content

Instantly share code, notes, and snippets.

View theKashey's full-sized avatar

Anton Korzunov theKashey

View GitHub Profile
@theKashey
theKashey / TipLocky.js
Created October 11, 2018 21:29
Click-outside handler
import React from 'react';
import Locky from 'react-locky';
const TipLocky = ({ group, toggle, enabled, children }) => (
<Locky
onEscape={toggle}
noDefault
events={{
click: 'report-only',
touchend: 'report-only',
{
// ...
resolve: {
alias: {
'react': 'preact-compat',
'react-dom': 'preact-compat',
// Not necessary unless you consume a module using `createClass`
'create-react-class': 'preact-compat/lib/create-react-class',
// Not necessary unless you consume a module requiring `react-dom-factories`
'react-dom-factories': 'preact-compat/lib/react-dom-factories'
@theKashey
theKashey / remock.js
Created October 2, 2018 11:08
Using react-remock to track "defenition of done"
import {remock, createElement} from 'react-mock';
// Locks on mount. Unlocks on unmount
class LockerInternal extends React.Component {
componentDidMount() {
this.props.locker.lock();
}
componentWillUnmount() {
this.props.locker.unlock();
@theKashey
theKashey / client-api.js
Last active April 4, 2019 08:30
using webpack alias to distinguish client and server for https://hackernoon.com/ssr-dependency-mocking-is-the-answer-d8d8c371aa94
export const getCustomerName = () => {
// Runs only in the client
return { name: 'Arunoda' }
}
@theKashey
theKashey / next3.js
Created September 30, 2018 11:46
next3
import { getCustomerName } from 'api/customer';
export default class Index extends React.Component {
static getInitialProps ({ req }) {
// calling the same API for client and Server
return getCustomerName(req);
}
}
@theKashey
theKashey / next2.js
Created September 30, 2018 11:41
Next.js
export default class Index extends React.Component {
static getInitialProps ({ req }) {
if (process.env.IS_SERVER && req) { // just ONE MORE condition
// !!!NOW!!! Runs only in the server, for sure
....
}
// Runs only in the client
}
}
@theKashey
theKashey / nextjs1.js
Created September 30, 2018 11:32
SSR-nextjs
export default class Index extends React.Component {
static getInitialProps ({ req }) {
if (req) {
// Runs only in the server
const faker = require('faker')
const name = faker.name.findName()
return { name }
}
// Runs only in the client
@theKashey
theKashey / babel.development.js
Created September 13, 2018 13:35
react-hot-loader babel 7 test
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
// the same as before
var PREFIX = '__reactstandin__';
var REGENERATE_METHOD = PREFIX + 'regenerateByEval';
var templateOptions = {
placeholderPattern: /^([A-Z0-9]+)([A-Z0-9_]+)$/
@theKashey
theKashey / foo.js
Last active August 27, 2018 16:04 — forked from ryanflorence/foo.js
Schrodinger Logic to show a modal
import {Phased} from 'recondition';
// Adds a lovely fade in of the modal
// and a gentle slide-down of the modal content
class Demo extends React.Component {
state = { showDialog: false };
render() {
return (
<div>
<button onClick={() => this.setState({ showDialog: true })}>
Show Dialog
// mouse in mouse case
// solve using "naked enzyme"
wrapper = shallow(
shallow(
<div>
{shallow(<App />)
.find(Mouse)
.prop('render')({ x: 2 })}
</div>