Skip to content
redux-props is useful to handle redux state changes. Implemented in Lua language.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
rockspecs
spec
src
.travis.yml
LICENSE
README.md

README.md

redux-props

Build Status

redux-props is useful for handling redux state changes.

Installation

You can install redux-props using LuaRocks:

$ luarocks install redux-props

Usage

Define componet:

--[[
    File: components/handler.lua
--]]
local Component = require 'redux-props.component'
local Handler = Component:extends()

function Handler:constructor(props)
    Component.constructor(Handler, props)
    -- something else
end

-- Define a props will change handler
function Handler:propsWillChange(prevProps, nextProps)
    -- handle props will change
end

-- Define a props changed handler
function Handler:propsDidChange()
    local props = self.props
    -- handle props changed
end

return Handler

Define container:

--[[
    File: containers/handler.lua
--]]
local connect = require 'redux-props.connect'
local handlerComp = require 'components.handler'
local Actions = require 'actions'

-- `ownProps` is optional
local function mapStateToProps(state, ownProps)
    return {
        url = state.example.url,
        flag = state.example.flag
    }
end

-- `ownProps` is optional
local function mapDispatchToProps(dispatch, ownProps)
    local props = {}
    function props.updateUrl(url)
        return dispatch(Actions.updateUrl(url))
    end

    function props.updateFlag(flag)
        return dispatch(Actions.updateFlag(flag))
    end

    return props
end


return connect(
    mapStateToProps,
    mapDispatchToProps
)(handlerComp)

Test dispatching:

local Provider = require 'redux-props.provider'
local ExampleActions = require 'actions.example'
local Handler = require 'container.handler'

local store = ...

Provider.setStore(store)

-- create handler instance with initial props.
local props = {}
local handler = Handler(props)

store.dispatch(ExampleActions.updateUrl('https://github.com'))

-- don't forget to call `destroy` before handler is destroyed.
handler:destroy()

License

MIT License

You can’t perform that action at this time.