Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Controllerim example #44

Closed
RusinovAnton opened this issue May 14, 2018 · 1 comment
Closed

Add Controllerim example #44

RusinovAnton opened this issue May 14, 2018 · 1 comment
Labels
help wanted Extra attention is needed

Comments

@RusinovAnton
Copy link
Contributor

https://github.com/Niryo/controllerim

A simple, clean and well structured state management library for react

Basic usage example

Note: This is a usage example of sharing data between parent and a direct child, but keep in mind that the child could have been a nested child and everything would have stayed the same.

Inside ParentController.js:

import { Controller } from 'controllerim';

export class ParentController extends Controller {
  constructor(compInstance) {
    super(compInstance);
    this.state = {
      message: 'hello' 
    };
  }
  getMessage() {
    return this.state.message;
  }
  setMessage(value) {
    this.state.message = value;
  }
}

Inside Parent.jsx:

import React, { Component } from 'react';
import { observer } from 'controllerim';
import { ParentController } from './ParentController';

class Parent extends Component {
  componentWillMount() {
    this.controller = new ParentController(this);
  }

  render() {
    return (
      <div>
        <h1>{this.controller.getMessage()}</h1>
        <Child/>
        <button onClick={() => this.controller.setMessage('hello world!')}>Click me to change message</button>
      </div>
    );
  }
};

export default observer(Parent);
/** 
note: If you don't want to use default export, you could export the class directly:
export const Parent = observer( class extends Component {
  ...
  }
);

*/

Inside Child.jsx:

import React, { Component } from 'react';
import { observer} from 'controllerim';
import { ChildController } from './ChildController';

class Child extends Component {
  componentWillMount() {
    this.controller = new ChildController(this);
    this.parentController = this.controller.getParentController('ParentController');
  }

  render() {
    return (
      <div>
        <span>This is a message from parent: {this.parentController.getMessage()}</span>
      </div>
    );
  }
};

export default observer(Child);
@GantMan GantMan added the help wanted Extra attention is needed label May 16, 2018
@juandc
Copy link
Contributor

juandc commented May 27, 2018

Help arrived!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants