An open API service indexing awesome lists of open source software.

https://github.com/ujjwalguptaofficial/godam-react


https://github.com/ujjwalguptaofficial/godam-react

Last synced: 2 months ago
JSON representation

Awesome Lists containing this project

README

          

# godam-react

Godam plugin for react framework.

# Installation

```
npm i godam-react
```

# Guide

## Using class based component

### Setup store

```
import React from "react";
import { initStore } from "godam-react";
import { Godam, Mutation, Expression } from "godam";

class RootMutation extends Mutation {
firstName(value) {
this.state.firstName = value;
}
lastName(value) {
this.state.lastName = value;
}
}

class RootExpression extends Expression {

get fullName() {
return `${this.get('firstName')} ${this.get('lastName')}`
}

constructor() {
super();
this.markComputed("fullName", "firstName", 'lastName');
}
}

const store = new Godam({
state: {
firstName: 'ujjwal',
lastName: 'gupta'
},
mutation: RootMutation,
expression: RootExpression
})

initStore(store, React);
```

### Using in component

```
import React from "react";
import { mapState, createState, mapExpression, mapMutation } from "godam-react";

export default class Layout extends React.Component {
constructor() {
super();
this.createState({
firstName: mapState('firstName'),
lastName: mapState('lastName'),
gender: 'male',
fullName: mapExpression('fullName')
});

this.createMethod({
setFirstName: mapMutation("firstName")
})
}

onFirstNameChange(e) {
this.setFirstName(e.target.value);
}

onLastNameChange(e) {
this.store.set("lastName", e.target.value);
}

onGenderChange(e) {
this.setState({ "gender": e.target.value })
}

render() {
return (




Full Name:


{this.state.fullName}




First Name:





Last Name:





Gender:




)
}
}
```

## Functional component

### Setup store

```
import React from "react";

import { initStore } from "godam-react";
import { Godam, Mutation, Expression } from "godam";

class RootMutation extends Mutation {
firstName(value) {
this.state.firstName = value;
}
lastName(value) {
this.state.lastName = value;
}
}

class RootExpression extends Expression {

get fullName() {
return `${this.get('firstName')} ${this.get('lastName')}`
}

constructor() {
super();
this.markComputed("fullName", "firstName", 'lastName');
}
}

export const store = new Godam({
state: {
firstName: 'ujjwal',
lastName: 'gupta'
},
mutation: RootMutation,
expression: RootExpression
})

initStore(store, React);
```

### Using in component

```
import React, { useState } from "react";
import { mapState, createState, mapExpression, createMethod, mapMutation } from "godam-react";

export default function () {
const { firstName, lastName, fullName, store } = createState({
firstName: mapState('firstName'),
lastName: mapState('lastName'),
fullName: mapExpression('fullName')
})

const { setFirstName } = createMethod({
setFirstName: mapMutation('firstName')
});

const [gender, setGender] = useState('gender');

const onFirstNameChange = (e) => {
setFirstName(e.target.value);
}

const onLastNameChange = (e) => {
store.set('lastName', e.target.value);
}

const onGenderChange = (e) => {
setGender(e.target.value);
}

return (




Full Name:


{fullName}




First Name:





Last Name:





Gender:




)
}
```