{"name":"react-simple-di","version":"1.2.0","description":"Simple Dependancy Injection Solution for React","repository":{"type":"git","url":"git+https://github.com/kadirahq/react-simple-di.git"},"license":"MIT","options":{"mocha":"--require scripts/mocha_runner lib/**/__tests__/**/*.js"},"scripts":{"prepublish":". ./scripts/prepublish.sh","lint":"eslint ./lib","lintfix":"eslint ./lib --fix","testonly":"mocha $npm_package_options_mocha","test":"npm run lint && npm run testonly"},"devDependencies":{"nodemon":"1.7.x","mocha":"2.x.x","chai":"3.x.x","eslint":"1.7.x","eslint-plugin-react":"3.x.x","babel-eslint":"4.x.x","eslint-plugin-babel":"2.x.x","babel-cli":"6.x.x","babel-core":"6.x.x","babel-polyfill":"6.x.x","babel-preset-es2015":"6.x.x","babel-preset-stage-2":"6.x.x","babel-preset-react":"6.x.x","babel-plugin-transform-runtime":"6.x.x","react":"^15.0.0","react-dom":"^15.0.0","react-addons-test-utils":"^15.0.0","enzyme":"^2.2.0"},"peerDependencies":{"react":"^0.14.0 || ^15.0.0"},"dependencies":{"babel-runtime":"6.x.x","hoist-non-react-statics":"1.x.x"},"gitHead":"db5000b041808909890d22024e54cc7a009aa0e8","bugs":{"url":"https://github.com/kadirahq/react-simple-di/issues"},"homepage":"https://github.com/kadirahq/react-simple-di#readme","_id":"react-simple-di@1.2.0","_shasum":"dde0e5bf689f391ef2ab02c9043b213fe239c6d0","_from":".","_npmVersion":"3.8.5","_nodeVersion":"4.4.0","_npmUser":{"name":"arunoda","email":"arunoda.susiripala@gmail.com"},"dist":{"shasum":"dde0e5bf689f391ef2ab02c9043b213fe239c6d0","tarball":"http://registry-npm.ywwl.com/react-simple-di/download/react-simple-di-1.2.0.tgz","integrity":"sha512-O9lOQRNKGTN7Y7taBZXAx97bkqY//8sqFb5uAVnjHxoHS8l1QZ7kFm//Ze++6VqqaKJu6p5no9V6SbHl9BjTfw==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIFI3I8VyNwly9zaGlHXZOoZDBxT6EWQ1V9fXt/j2wuMZAiEA6NDok/q9yPneGcX3JOhzUDe60vidxmvUQDnWAQhavJk="}],"size":3928,"noattachment":false,"key":"/react-simple-di/-/react-simple-di-1.2.0.tgz"},"maintainers":[{"name":"arunoda","email":"arunoda.susiripala@gmail.com"}],"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/react-simple-di-1.2.0.tgz_1460206299438_0.3790955590084195"},"directories":{},"readme":"# react-simple-di\n\nSimple dependancy injection solution for React.\n\n### Installation\n\n```\nnpm i react-simple-di\n```\n\n### Intro\n\nIn `react-simple-di`, we've two types of dependencies, they are:\n\n1. context - These are usually, configurations, models and client for different remote data solutions.\n2. actions - Actions are simple functions which used to perform business logic with the help of the above context.\n\n> Every action will receive the `context` as it's first argument.\n\n### Injecting Dependancies\n\nFirst, we need to inject dependencies to a root level React component. Mostly, this will be the main layout component of our app.\n\nHere are our dependencies:\n```js\nconst context = {\n    DB,\n    Router,\n    appName: 'My Blog'\n};\n\nconst actions = {\n    posts: {\n        create({DB, Router}, title, content) {\n            const id = String(Math.random());\n            DB.createPost(id, title, content);\n            Router.go(`/post/${id}`);\n        }\n    }\n};\n```\n\nFirst we've defined our context. Then, we have our actions. Here actions must follow a structure like mentioned above.\n\nLet's inject our dependencies:\n\n```js\nimport {injectDeps} from 'react-simple-di';\nimport Layout from './layout.jsx';\n\n// Above mentioned actions and context are defined here.\n\nconst LayoutWithDeps = injectDeps(context, actions)(Layout);\n```\n\nNow you can use `LayoutWithDeps` anywhere in your app.\n\n## Using Depedencies\n\nAny component rendered inside `LayoutWithDeps` can access both context and actions. \n\nWhen using dependecies it will compose a new React component and pass dependencies via props to the original component.\n\nFirst let's create our UI component. Here it will expect dependecies to come via props `appName` and `createPost`.\n\n```js\nclass CreatePost extends React.Component {\n    render() {\n        const {appName} = this.props;\n        return (\n            <div>\n                Create a blog post on app: ${appName}. <br/>\n                <button onClick={this.create.bind(this)}>Create Now</button>\n            </div>\n        );\n    }\n\n    create() {\n        const {createPost} = this.props;\n        createPost('My Blog Title', 'Some Content');\n    }\n}\n```\n\nSo, let's use dependencies:\n\n```js\nconst {useDeps} from 'react-simple-di';\n\n// Assume above mentioned CreatePost react component is\n// defined here.\n\nconst depsToPropsMapper = (context, actions) => ({\n    appName: context.appName,\n    createPost: actions.posts.create\n});\n\nconst CreatePostWithDeps = useDeps(depsToPropsMapper)(CreatePost);\n```\n\nThat's it. \n\n> Note: Here when calling the `actions.posts.create` action, you don't need to provide the context as the first argument. It'll handle by `react-simple-di`.\n\n**Default Mapper**\n\nIf you didn't provide a mapper function, useDeps will use a default mapper function will allows you to get context and props directy. Here's that default mapper:\n\n```js\nconst mapper = (context, actions) => ({\n    context: () => context,\n    actions: () => actions\n});\n```\n","publish_time":1460206299871,"_cnpm_publish_time":1460206299871,"dist-tags":{"latest":"1.2.0"}}