Giter Site home page Giter Site logo

react-vue-component's Introduction

react-vue-component

Build Vue-like components in React. Get the goodness of Vue-reactivty system such as watchers and computed properties and remove the need of using React's setState completely.

Installation

$ npm install --save react-vue-component

Basic  Usage

  • import {Component} from "react-vue-component";
    
    class App extends Component {
        state = {
            name: "Bob", 
        }
        componentDidMount() {
            this.name = "Albert";
        }
        watch {
            name(newName, oldName) {
                console.log("name has changed!");
            },
        }
        computed {
            fullName(){
                return this.name + " Lolzer";
            }
        }
        methods {
            changeName() {
                // Changing name will also change the computed property fullName
                this.name = "John";
            }
        }
        render() {
            // states, methods, and computed properties can be accessed directly via `this` just like in Vue
            const {name, fullName, changeName} = this;
            
            return (
                <div>
                    <p>{name}</p>
                    <p>{fullName}</p>
                    <a onClick={() => this.changeName()}>Change my name</a>  
                </div>   
            )
        }
    }

Objects

  • In Vue, in order to "reactively" add/delete key-value pairs from an object, you will need to use  set(obj, key, value)and delete(obj,key)respectively.
import {Component} from "react-vue-component";
class App extends Component {
  state = {
      obj : {
          name: "Prev"
      }
  }
  methods {
      addToObj() {
          this.set(this.obj, "age", 20); 
      }
      deleteName() {
          this.delete(this.obj, "name");
      }
  }
  watch {
      // nested watchers
      "obj.name" : (newName, oldName) => {
          console.log("Obj.name has changed");
      }
      "obj.age" : (newAge, oldAge) => {
          console.log("Obj.age has changed");
      }

  }
  render() {
      const {obj} = this;
      return (
          <div>
              Object.keys(obj).map(key => 
                  <p><strong>{key}</strong>: {obj[key]}</p>
              ) 
            <a onClick={() => this.addToObj()}>Add new key</a>
            <a onClick={() => this.deleteName()}>Delete name</a>
          </div>
      )
  }
}

Caveats

  • Behind the scenes,componentWillMount is being used to inject Vue's reactivity system thus it is made impossible to overwrite. Instead, use beforeMount as an equivalent replacement:

  • import {Component} from "react-vue-component";
    class App extends Component {
        beforeMount(){
            console.log("before mount! component hasnt render yet...")
        }
        ...
    }

Credits

  • All credits to the Vue core team for their awesome reactivity system.

react-vue-component's People

Contributors

prevwong avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.