0.0.6 ā€¢ Published 6 years ago

bs-react-notification-system v0.0.6

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

Reason bindings for react-notification-system

npm

Reason bindings for react-notification-system.

Status

šŸš§ This is a WIP, not everything is supported yet. šŸš§

Feel free to create an issue or PR if you find anything missing.

Installation

yarn add bs-react-notification-system
yarn add react-notification-system@0.2.x

Then add bs-react-notification-system to bs-dev-dependencies in your bsconfig.json:

{
  ...
  "bs-dev-dependencies": ["bs-react-notification-system"]
}

Usage

type action =
  | AddNotification(string);

type state = {
    _notificationSystem: ref(option(ReasonReact.reactRef)),
};

let setNotificationSystemRef = (notificationRef, {ReasonReact.state}) => 
  state._notificationSystem := Js.Nullable.to_opt(notificationRef) ;

let component = ReasonReact.reducerComponent("Notifications");

let addNotification = (message, state) => {   
    switch state._notificationSystem^ {
    | None => ()
    | Some(r) => ReasonReact.refToJsObj(r)##addNotification({"message": message, "level": "success"});      
    }
};

let make = (_children) => {
    ...component,
    initialState: () => {_notificationSystem: ref(None) },
    reducer: (action, state) =>
        switch action {
            | AddNotification(message) =>  ReasonReact.SideEffects(((_) => addNotification(message, state)))
        },
    render: ({handle, reduce}) => (
    <div>             
        <ReactNotificationSystem ref=(handle(setNotificationSystemRef)) />
        <button onClick=(reduce( (_) => AddNotification("Hello"))) > (ReasonReact.stringToElement("Click")) </button> 
    </div>
  )
};

Bindings

  • NotificationSystem