-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Welcome to the Rydux documentation. If you see something that looks outdated or wrong, submit a pull request!
Subscribes the listener to the store. Any time the state store changes, the listener's #state_changed(state)
method will be called. Any instance can be a listener, as long as it implements the #state_changed
method.
Example:
class MyListener
def initialize
# Assuming you've initialized a store called MyStore...
MyStore.subscribe(self)
end
def state_changed(state)
puts "Got new state: #{state}"
end
end
Unsubscribes the specified listener from the store. This listener will not receive updates to the store's state once it has unsubscribed.
Example:
In the following scenario, MyListener unsubscribes from the store as soon as it receives the first update to the state.
class MyListener
def initialize
# Assuming you've initialized a store called MyStore...
MyStore.subscribe(self)
end
def state_changed(state)
puts "Got new state: #{state}"
MyStore.abandon(self)
end
end
Dispatches an action to all reducers. Takes in an action and an optional callback proc, which will get called once the dispatch has finished. Any keys passed to #dispatch
will be passed in to the reducers #map_state
method, as part of the action
parameter.
Example:
#dispatch
can be called in a variety of ways, depending on what is more syntactically convenient. Each format performs the exact same actions. Valid ways of calling #dispatch
are:
# Passing in a hash
MyStore.dispatch({ type: 'MY_ACTION' })
MyStore.dispatch({ type: 'MY_ACTION', payload: [1, 2, 3] })
MyStore.dispatch({ type: 'My_ACTION', some_other_key: 'some value' })
# Hash syntax with optional callback proc
MyStore.dispatch({ type: 'MY_ACTION' }, ->{ puts "Action done dispatching" })
# Keyword syntax
MyStore.dispatch(type: 'MY_ACTION', payload: 'some payload', ->{ puts "Action done dispatching" })
# Argument syntax (first parameter is type, second parameter is the payload, and will be mapped to action.payload)
MyStore.dispatch('MY_ACTION')
MyStore.dispatch('MY_ACTION', { some_key: 'some value' })
MyStore.dispatch('MY_ACTION', { some_key: 'some value' }, ->{ puts "Action done dispatching" })
MyStore.dispatch('MY_ACTION', ->{ puts "Action done dispatching" })
Each reducer must have a map_state
class method. The action that you pass in via the Store's dispatch method will be available here as the first parameter of the function. The latest state from the store will also be passed in as the second argument. Any modifications you make to the state here will not be reflected in the store. Only the return value of this method will affect the store. It is recommended to return a merged version of the state, unless you plan on overwriting the existing state.
Example:
class MyReducer < Rydux::Reducer
@@initial_state = {
my_key: 'My Value'
}
def self.map_state(action, state = @@initial_state)
# Perform a different mutation to the state depending on
# what the action was
case action[:type]
when 'MY_ACTION'
state.merge(new_key_in_store: action[:payload]
else
# This else is IMPORTANT. If you don't return anything
# from your reducer, the state will be set to nil.
# Always return the state if you don't want to modify it.
state
end
end
end