rctx
v1.0.5
Published
Context tools to React Context API
Downloads
2
Readme
Rctx
Example
//Test component
import { importContexts } from 'rctx';
import TestContext from './test-context.ctx';
const TestComponent = props => {
const { testContext } = props;
const { add } = testContext;
return <div onClick={add}>hello world</div>;
};
export default injectContexts(TestComponent, {
testContext: TestContext
});
//Test Context
import { ContextComponent createContext ContextStore } from 'rctx';
class TestContext extends ContextComponent{
state = {value:0}
add = () => {
this.setState(prevState => {
return { value: prevState.value + 1 };
});
}
}
export default createContext(TestContext, { store: new ContextStore() /*If you want to preserve the state when component unmount*/ });
//App
import TestContext from './test-context.ctx';
import { importContexts } from 'rctx';
class App extends Component {
render() {
return <TestComponent />;
}
}
export default importContexts(App, [TestContext]);