1.0.70 • Published 4 years ago

flux-native v1.0.70

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

flux-native

Features

  1. Easy to use and simple APIs, no reducer, no provider, no action type, no 3rd lib needed for async dispatching
  2. Support advance action handling: cancel, suspend, resume
  3. Support action transaction
  4. Support state merging
  5. Support multiple action dispatching ways: once, on component mount, on component unmount

Counter App

import React from "react";
import { render } from "react-dom";
import { dispatch, useStore } from "flux-native";

const Initial = () => ({ count: 1 });

dispatch(Initial);

const Increase = state => ({ ...state, count: state.count + 1 });

const App = () => {
  const [count] = useStore(state => state.count);
  const handleIncrease = () => dispatch(Increase);
  return (
    <>
      <h1>{count}</h1>
      <button onClick={handleIncrease}>Increase</button>
    </>
  );
};

render(<App />, document.getElementById("root"));

Search App

import React from "react";
import { render } from "react-dom";
import { dispatch, useStore } from "flux-native";

const Initial = () => ({ keyword: "", results: [] });

dispatch(Initial);

const Fetch3Items = state => ({
  ...state,
  results: ["result 1", "result 2", "result 3"]
});

const NotFound = state => ({ ...state, results: [] });

// save keyword to sate
const UpdateKeyword = (state, keyword) => ({ ...state, keyword });

const FetchSearchResults = state => ({ dispatch, check }) => {
  // enable state checking before dispatching
  check((current, prev) => {
    // perform dispatching if keyword does not change
    return current.keyword === prev.keyword;
  });

  if (state.keyword === "abc") {
    setTimeout(dispatch, 1000, Fetch3Items);
  } else {
    dispatch(NotFound);
  }
};

const Search = (state, keyword) => ({ dispatch }) => {
  dispatch(UpdateKeyword, keyword);
  dispatch(FetchSearchResults);
};

const App = () => {
  const [results] = useStore(state => state.results);
  const handleSearch = e => dispatch(Search, e.target.value);
  return (
    <>
      <input
        type="text"
        onChange={handleSearch}
        placeholder="Enter search term (ex: abc)"
      />
      <ul>
        {results.map(result => (
          <li key={result}>{result}</li>
        ))}
      </ul>
    </>
  );
};

render(<App />, document.getElementById("root"));

Monitoring and handling action dispatching status

import React, { useRef, useState } from "react";
import { render } from "react-dom";
import { dispatch, useStore } from "flux-native";

const LoadData = () => ({ merge, onCancel }) => {
  // mark data is loading
  merge({ loading: true });

  onCancel(() => {
    // mark nothing to load
    merge({ loading: false, data: undefined });
  });

  // assign test data after 2s
  setTimeout(merge, 2000, {
    loading: false,
    data: "Data loaded " + Math.random().toString(16)
  });
};

const App = () => {
  const [data, loading] = useStore(state => state.data, state => state.loading);
  const loadDataHandler = useRef();
  const [, forceRerender] = useState();
  const handleStatusChanged = () => {
    forceRerender({});
  };
  const handleLoad = () => {
    loadDataHandler.current = dispatch(LoadData);
    // register status changed listener
    loadDataHandler.current.onStatusChange(handleStatusChanged);
  };
  const handleCancel = () => {
    loadDataHandler.current && loadDataHandler.current.cancel();
  };
  const handleSuspend = () => {
    loadDataHandler.current && loadDataHandler.current.suspend();
  };
  const handleResume = () => {
    loadDataHandler.current && loadDataHandler.current.resume();
  };
  return (
    <>
      <p>
        <button onClick={handleLoad}>Load data</button>
        {loading && (
          <>
            <button onClick={handleCancel}>Cancel</button>
            {loadDataHandler.current.suspended() ? (
              <button onClick={handleResume}>Resume</button>
            ) : (
              <button onClick={handleSuspend}>Suspend</button>
            )}
          </>
        )}
      </p>
      <p>
        {loadDataHandler.current && loadDataHandler.current.suspended()
          ? "Suspended"
          : loading
          ? "Loading..."
          : data}
      </p>
    </>
  );
};

render(<App />, document.getElementById("root"));

Dispatching action once

import { dispatchOnce } from "flux-native";

const Initialize = () => ({ data: [] });

dispatchOnce(Initialize);

Dispatching action when component mount or unmount

import React from "react";
import { dispatchOnMount, dispatchOnUnmount } from "flux-native";

const OnComponentMount = (state, ...args) => {
  console.log("component mount", args);
};
const OnComponentUnmount = (state, ...args) => {
  console.log("component unmount", args);
};

const MyComponent = () => {
  dispatchOnMount(OnComponentMount, [1, 2, 3], ["deps"]);
  dispatchOnUnmount(OnComponentUnmount, [1, 2, 3], ["deps"]);
  return <div />;
};

Handle state change

import { getState, subscribe } from "flux-native";

const unsubscribe = subscribe(() => {
  console.log('state changed', getState());
});

// unsubscribe()
1.0.70

4 years ago

1.0.69

4 years ago

1.0.68

4 years ago

1.0.67

4 years ago

1.0.62

4 years ago

1.0.66

4 years ago

1.0.65

4 years ago

1.0.64

4 years ago

1.0.63

4 years ago

1.0.61

4 years ago

1.0.60

4 years ago

1.0.59

4 years ago

1.0.58

4 years ago

1.0.57

4 years ago

1.0.56

4 years ago

1.0.55

4 years ago

1.0.54

4 years ago

1.0.53

4 years ago

1.0.52

4 years ago

1.0.51

4 years ago

1.0.50

4 years ago

1.0.48

4 years ago

1.0.49

4 years ago

1.0.47

4 years ago

1.0.46

4 years ago

1.0.45

4 years ago

1.0.44

4 years ago

1.0.43

4 years ago

1.0.42

4 years ago

1.0.41

4 years ago

1.0.39

4 years ago

1.0.40

4 years ago

1.0.38

4 years ago

1.0.37

4 years ago

1.0.36

5 years ago

1.0.35

5 years ago

1.0.34

5 years ago

1.0.33

5 years ago

1.0.32

5 years ago

1.0.31

5 years ago

1.0.30

5 years ago

1.0.29

5 years ago

1.0.28

5 years ago

1.0.27

5 years ago

1.0.26

5 years ago

1.0.25

5 years ago

1.0.24

5 years ago

1.0.23

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago