Skip to content

Instantly share code, notes, and snippets.

@malash
Created September 14, 2017 08:22
Show Gist options
  • Save malash/36d05b4cc04670a70a5f1f8a786a80f9 to your computer and use it in GitHub Desktop.
Save malash/36d05b4cc04670a70a5f1f8a786a80f9 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import { connect, state } from '@noflux/react';
const traversal = (node, callback) => {
React.Children.map(node, child => {
callback(child);
if (child.props && child.props.children) {
traversal(child.props.children, callback);
}
})
};
class Provider extends Component {
render() {
const {
children,
} = this.props;
traversal(children, component => {
if (typeof component === 'string') {
return;
}
if (typeof component.type === 'string') {
return;
}
if (!component.type.__noflux) {
connect(component.type);
}
})
return React.Children.only(children);
}
}
state.set('data', 0);
class App extends Component {
render() {
return (
<div>
{state.get('data')}
<button onClick={() => state.set('data', state.get('data') + 1)}>+1</button>
</div>
)
}
}
export default class TestPage extends Component {
render() {
return (
<Provider>
<div>
<span>1</span>
<App />
<span>3</span>
</div>
</Provider>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment