Refract
  • Read Me
  • Introduction
    • Why Refract?
    • Core Concepts
    • Thinking In Refract
    • Alternatives
  • Usage
    • Getting Started
    • Installation
    • Connecting To React
    • Observing React, Preact and Inferno
    • Injecting Dependencies
    • Observing Redux
    • Observing Anything
    • Handling Effects
    • Pushing to Props
    • Rendering Components
    • Handling Errors
    • Testing
  • Recipes
    • Dependency Injection
    • Creating An API Dependency
    • Handling state
    • Replacing Redux Connect
  • Examples
    • Debounced fetch
    • Counter
    • Field validation
    • Redux fetch
    • Routing
    • Typeahead
    • Visit time
  • API
    • compose
    • withEffects
    • toProps, asProps
    • useRefract
    • refractEnhancer (Redux)
  • Glossary
  • Feedback
Powered by GitBook
On this page
  • Counter
  • Aperture
  • Handler
  • Result
  1. Examples

Counter

PreviousDebounced fetchNextField validation

Last updated 6 years ago

Counter

callbag

most

RxJS

xstream

This basic example involves a counter which the user can toggle between incrementing, decrementing, and paused states. The parent component has a simple state: { counter: 0, direction: 'NONE' }. The buttons in the UI alter the direction state.

Aperture

Two sources are observed:

  • The setDirection callback function.

  • An interval which dispatches a new number every second.

Every time setDirection is called, the value it was called with is dispatched.

These two sources are then merged; the resulting stream contains a new effect every second, which contains the current direction as its type.

Handler

Simply increments or decrements the state whenever an effect is dispatched with the correct effect type.

Result

The end result is a counter which infinitely counts upwards after the user clicks increase, infinitely counts downwards after the user clicks decrease, or stays static after the user clicks pause.

code
live
code
live
code
live
code
live