Skip to content

Instantly share code, notes, and snippets.

@dtothefp
Last active February 20, 2016 16:56
Show Gist options
  • Select an option

  • Save dtothefp/3f2289aa2cd6c5be4500 to your computer and use it in GitHub Desktop.

Select an option

Save dtothefp/3f2289aa2cd6c5be4500 to your computer and use it in GitHub Desktop.
const daisyChain = (
<DaisyChain
reactor={reactor}
Actions={Actions}
Getters={Getters}
analytics={analytics}
daisyChainId={daisyChainId}
legacy
>
<EmailSignupForm ctaId={ctaId} />
<SingleIssueForm ctaId={ctaId} >
</DaisyChain>
);
ReactDOM.render(
daisyChain,
document.querySelector(selectors)
);
const compToRender = (
<Wrapper
reactor={reactor}
Actions={Actions}
analytics={analytics}
legacy
>
<div class="o-wrap-content">
<SignupForm />
<SingleIssueForm />
</div>
<DaisyChain />
</Wrapper>
);
ReactDOM.render(compToRender, '[data-some-element]');
export default function(YourCtaComponent, dataBindingsFn) {
static contextTypes = {
reactor: PropTypes.object.isRequired
};
constructor(props, context) {
super(props);
const {reactor} = context;
//this is the object you return from `nuclearComponent` decorator
const dataBindings = this.dataBindings = dataBindingsFn(props);
const yourStoreState = Object.keys(dataBindings).reduce((acc, propKey) => {
const keyPath = dataBindings[propKey];
//evaluates your initial state in the store
acc[propKey] = reactor.evaluate(keyPath);
return acc;
}, {});
this.state = yourStoreState;
}
componentDidMount() {
const {reactor} = this.context;
Object.keys(this.dataBindings).forEach(propKey => {
const keyPath = this.dataBindings[propKey];
//setup observation of the store, unfortunately components `didMount` get's called from the inside out
//ie. child => parent so if you decide to emit an action in the `child` `constructor` or `componentDidMount`
//then state will get out of sync and that action will not trigger this `reactor.observe` state update
reactor.observe(keyPath, (changedValFromStore) => {
const newState = Object.assign({}, this.state, {
[propKey]: changeValFromStore
});
this.setState(newState); //this updates Higher Order Component's state and causes re-render passing state as props to child
});
});
}
render() {
const mergedProps = Object.assign({}, this.props, this.context, this.state);
return <YourCtaComponent {...mergedProps} />;
}
}
import {provideContextProps} from '@hfa/form-components/lib/config';
import {provideReactor} from 'nuclear-js-react-addons';
const ComponentToRender = provideReactor(SignupForm, provideContextProps);
const comp = (
<ComponentToRender
reactor={reactor}
Actions={Actions}
analytics={analytics}
legacy
/>
);
ReactDOM.render(comp, someDomNode);
@provideReactor(provideContextProps)
class Wrapper extends Component {
static propTypes = {
children: PropTypes.array.isRequired
};
render() {
<div>
{this.props.children}
</div>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment