项目作者: afeiship

项目描述 :
Micro context for react project.
高级语言: JavaScript
项目地址: git://github.com/afeiship/micro-context.git
创建时间: 2019-11-13T13:16:13Z
项目社区:https://github.com/afeiship/micro-context

开源协议:MIT License

下载


micro-context

Micro context for react project.

usage

  1. // app.js
  2. import React from 'react';
  3. import Button from '@/components/button';
  4. import Card from '@/components/card';
  5. import { MicroApp } from 'micro-context';
  6. export default class extends MicroApp {
  7. static initialState() {
  8. return {
  9. memory: {
  10. home: {
  11. rand: 'randstr'
  12. }
  13. }
  14. };
  15. }
  16. getApp() {
  17. return (
  18. <div className="hello">
  19. <h1>Hello world!</h1>
  20. <p>It works!</p>
  21. <Button ></Button>
  22. <Card ></Card>
  23. </div>
  24. );
  25. }
  26. }
  27. // card.js
  28. import React from 'react';
  29. import { connect } from 'micro-context';
  30. @connect
  31. export default class extends React.Component {
  32. componentDidMount() {
  33. const { app } = this.props;
  34. this.btnRes = app.on('button:click', ({ value }) => {
  35. console.log('card received!', value);
  36. });
  37. }
  38. componentWillUnmount() {
  39. this.btnRes.destroy();
  40. }
  41. render() {
  42. const { memory } = this.props;
  43. return <div>Card Component {memory.home.rand}</div>;
  44. }
  45. }
  46. // card.js
  47. import React from 'react';
  48. import { connect } from 'micro-context';
  49. @connect
  50. export default class extends React.Component {
  51. componentDidMount() {
  52. const { app } = this.props;
  53. this.btnRes = app.on('button:click', ({ value }) => {
  54. console.log('card received!', value);
  55. });
  56. }
  57. componentWillUnmount() {
  58. this.btnRes.destroy();
  59. }
  60. render() {
  61. const { memory } = this.props;
  62. return <div>Card Component {memory.home.rand}</div>;
  63. }
  64. }