ryardley / ts-bus

A lightweight JavaScript/TypeScript event bus to help manage your application architecture.
MIT License
135 stars 8 forks source link

Docs: useBusReducer possibly a little confusing #3

Closed djensen47 closed 5 years ago

djensen47 commented 5 years ago

I would suggest a slightly more filled out example like this, which I'm basically pulling directly from the React Hook docs ...

import { useBusReducer } from "ts-bus/react";

function Main(props: Props) {
  // Automatically hook into bus passed in with
  // BusProvider above in the tree
  const initState = { counter: 0 };
  const state = useBusReducer(initState, (state, action) => {
    switch (action.type) {
      case "COUNTER_INCREMENT": {
        return {counter: state.counter + 1};
      }
      case "COUNTER_DECREMENT": {
        return {counter: state.counter - 1};
      }
      default:
        return state;
    }
  });

  return <>Count: {state.counter}</>;
}

For whatever reason, I was getting hung up on <MyApp> and passing it state.

djensen47 commented 5 years ago

Actually, maybe the counter isn't the best example in this case. But something more concrete would be nice.

ryardley commented 5 years ago

Thanks! I have written a small application to demonstrate the lib. Will update it as an example soon and would be happy to provide a better example in the docs.

ryardley commented 5 years ago

I just added the example app if you want to throw up a PR for the docs that would be amazing otherwise I will probably look at it next week when I get time.

ryardley commented 5 years ago

Updated docs.