0.5.2 • Published 4 years ago

react-axiom v0.5.2

Weekly downloads
279
License
ISC
Repository
github
Last release
4 years ago

React Axiom

React Axiom is a way to use models with React.

npm version Build Status codecov

Links

Getting Started

Install React Axiom:

npm install --save react-axiom

To build a model class for an application, import and extend the React Axiom Model class:

import { Model } from 'react-axiom';

class Task extends Model {};

const task1 = new Task({
  completed: false,
  description: 'Hello world'
});

When instantiating a model, the Model base class will meta-program helper methods to set and access state. See the model reference for more information on these helper methods. For this particular example they are the following:

task1.isCompleted();
//=> false

task1.setCompleted(true);
task1.isCompleted();
//=> true

task1.getDescription();
//=> 'Hello world'

task1.hasDescription();
//=> true

task1.setDescription('Hello again');
task1.getDescription();
//=> 'Hello again'

Create a React component to display the task model and wrap the component with the higher-order React Axiom subscribe() function to ensure that the React component will re-render when the model's state changes:

import React from 'react';
import { subscribe } from 'react-axiom';

class TaskComponent from React.Component {
  render() {
    const { task } = this.props;
    return (
      <li>
        <input
          type="checkbox"
          onClick={() => task.setCompleted(true)}
          checked={task.isCompleted()}
        />
        {task.getDescription()}
      </li>
    );
  }
}

const TaskSubscriber = subscribe(TaskComponent);

Finally, use the wrapped component like any other React component and pass the model instance as a property:

ReactDOM.render(<TaskSubscriber task={task1} />);
0.5.2

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.2

5 years ago

0.4.1

5 years ago

0.4.0

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.5

7 years ago

0.2.4

7 years ago

0.2.3

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.17

7 years ago

0.0.16

8 years ago

0.0.15

8 years ago

0.0.13

8 years ago

0.0.12

8 years ago

0.0.11

8 years ago

0.0.10

8 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago