🗡️
ConanJs
  • What is ConanJs?
  • Why ConanJs...
    • ... if coming from Redux
    • ... if using vanilla React
    • ... if learning React
    • ... if not using React
  • How to install/use ConanJs
  • About us / Github / Contact us
  • Demos
    • Demo Gallery
    • Conan State Demos
      • Hello World
      • Todos
        • Todos - Basic
        • Todos - Async
        • Todos - Optimistic
      • Github issues viewer
    • Conan Flow Demos
      • Authentication
  • CONAN DATA
    • General Concepts
    • Conan State
      • Actions & Reducers
        • Reducers
        • Actions
      • Creating State
      • Observing State
        • Live rendering
        • Connecting
      • Composing State
      • Scaling State
      • Orchestrating State
      • Life cycle
        • Async handling
        • Introspection
      • Testing state
    • Conan Flow
      • Creating Flows
      • Serialising Flows
      • Observing Flows
  • CONAN Runtime
  • Dependency Injection
    • General Concepts
    • Creating the Context
    • Using the Context
  • ASAPs
  • Logging
  • API
    • Main Classes
      • Conan
        • StateDef
      • ConanState
      • ConanFlow
        • UserFlowDef
        • UserStatusDef
        • Status
    • Conan State Classes
      • Thread
      • ConnectedState
      • MonitorInfo
      • MetaInfo
    • Dependency Injection
      • DiContextFactory
    • ASAPS
      • Asaps
      • Asap
Powered by GitBook
On this page
  • Introduction
  • Conan.light
  • Conan.state
  • Guidelines
  • Demo

Was this helpful?

  1. CONAN DATA
  2. Conan State

Creating State

PreviousActionsNextObserving State

Last updated 4 years ago

Was this helpful?

Introduction

In ConanJs we advocate for splitting the state in its atomic representation and use as much as possible and to if you need to combine/derive it.

Point to Demo! TBC

To help with this, we provide with two main flavours to create state, Conan.light and Conan.state. You can see the details in the

Conan.light

Is the simplest way to create state.

Conan.light just receives two parameters:

  1. state name: a string used to identify this state

  2. the initial value

const yetAnotherCounter$ = Conan.light('counter', 0)

You can see this in our demo.

Conan.state

It is the full fledge option to create state, if you have to use this method to create state you will have to pass a StateDef object.

The object is described with detail in its API section , but you will likely be interested in passing to it.

Conan State only with reducers:

export const todoListSyncState$ = Conan.state({
    name: 'todos-sync',
    initialData: {
        appliedFilter: VisibilityFilters.SHOW_ALL,
        todos: []
    },
    reducers: getState=>({
        $addTodo: (todo) => ({
            todos: [...getState().todos, todo],
            appliedFilter: getState().appliedFilter
        })
    }),
});

Conan State with reducers and action:

export const repoDetailsState$: RepoDetailsState = Conan.state<RepoDetailsData>({
    name: 'repo-details',
    initialData: {openIssuesCount: -1, error: null},
    reducers: repoDetailsReducersFn,
    actions: repoDetailsActionsFn
});

Guidelines

Demo

You can see how to create a Conan State with reducers in our demo.

You can see how to create a Conan State with reducers and actions in our demo.

1- If possible use Conan.light first. We recommend to have atomic state, and to when needed

2- If you need to create a full fledged ConanState, follow the . Similar to the previous point, we recommend to simplify the states that you create on your application.

compose state
compose state
API section for Conan
Hello Wow!
StateDef
Actions & Reducers
guidelines for creating reducers and actions
Github
Todos
the default actions