Skip to content

Instantly share code, notes, and snippets.

@xialvjun
xialvjun / cache-react-component.js
Created August 9, 2016 09:08
Wrap a component class and get the CachedComponent. This CachedComponent will calculate a key from props and use the key to decide whether to construct a new instance and cache the old or recover the old component from cache and update props. We can use it with react-router easily to save up the time of constructing the RouteComponent you alread…
import React from 'react'
export default function cache(mapPropsToKey, option) {
mapPropsToKey = mapPropsToKey || (() => 'NoCache')
option = option || {}
// 默认最高5层,1000年
const {
count=5,
expire=3153600000000,
@xialvjun
xialvjun / react-flyd-binding.js
Created July 21, 2016 09:06
Inspired by theadam/react-flyd. Allows for flyd streams to be embedded directly into JSX, and to update content when the streams fire events.
import { Component, createElement, cloneElement, isValidElement } from 'react'
import { isStream, on, combine } from 'flyd'
export function reactive(tag='') {
class ReactiveClass extends Component {
constructor(props) {
super(props)
this.displayName = `ReactiveClass-${typeof tag==='string' ? tag : (tag.displayName || tag.name || '')}`;
this.state = {}
}