UNPKG

react-ko

Version:

React + Knockout integration library

200 lines (144 loc) • 4.44 kB
# react-ko en English | [ja ę—„ęœ¬čŖž](./README.ja.md) [![npm version](https://img.shields.io/npm/v/react-ko)](https://www.npmjs.com/package/react-ko) > 🧠 A minimal bridge to use Knockout.js inside React components. > Combine Knockout's reactivity with React's component architecture — clean, scoped, and type-safe. --- ## ✨ Features - āœ… Seamless two-way data binding with Knockout observables - āœ… Use `data-bind="..."` directly in JSX / TSX - āœ… Scoped ViewModel logic via `<KnockoutScope>` - āœ… One-line root binding via `<RootKnockoutProvider>` - āœ… Zero boilerplate — no event handlers or local state - āœ… Full TypeScript & JavaScript support with zero-config - āœ… No runtime dependencies other than Knockout & React --- ## šŸ“¦ Installation ```bash npm install react-ko knockout ``` > āš ļø This library requires `react` (v18+) and `knockout` (v3.5+) as peer dependencies. --- ## ⚔ Quick Start with Starter Template ### ā–¶ TypeScript ```bash npx degit menimani/react-ko-starter-ts my-app-ts cd my-app-ts npm install && npm run dev ``` šŸ”— GitHub: [react-ko-starter-ts](https://github.com/menimani/react-ko-starter-ts) --- ### ā–¶ JavaScript ```bash npx degit menimani/react-ko-starter-js my-app-js cd my-app-js npm install && npm run dev ``` šŸ”— GitHub: [react-ko-starter-js](https://github.com/menimani/react-ko-starter-js) --- ## šŸš€ Quick Usage (JSX / TSX) ```tsx import ko from 'knockout' import { RootKnockoutProvider, KnockoutScope } from 'react-ko' const viewModel = { name: ko.observable('Alice') } <RootKnockoutProvider viewModel={{}}> <KnockoutScope viewModel={viewModel}> <input data-bind="value: name" /> </KnockoutScope> </RootKnockoutProvider> ``` --- ## 🧩 Custom Component Example ### ā–¶ļø JavaScript (JSX) ```jsx import { KnockoutScope } from 'react-ko' export function KoInput({ value }) { const vm = { value } return ( <KnockoutScope viewModel={vm}> <input data-bind="value: value" /> </KnockoutScope> ) } ``` ### ā–¶ļø TypeScript (TSX) ```tsx import ko from 'knockout' import { KnockoutScope } from 'react-ko' type Props = { value: ko.Observable<string> } export function KoInput({ value }: Props) { const vm = { value } return ( <KnockoutScope viewModel={vm}> <input data-bind="value: value" /> </KnockoutScope> ) } ``` ### ā–¶ļø Component Usage ```tsx const vm = { name: ko.observable('Alice') } <KnockoutScope viewModel={vm}> <KoInput value={vm.name} /> </KnockoutScope> ``` ### ā— Deprecated Components > āš ļø The following components are deprecated and will be removed in a future release (v2.0.0): > > - `KoIfComment` > - `KoIfNotComment` > - `KoForeachComment` > > Please use the unified components instead: > > - āœ… `KoIf` > - āœ… `KoIfNot` > - āœ… `KoForeach` > > These new components are fully JSX-compliant and no longer rely on HTML comment nodes. --- ## šŸ¤” Why react-ko? Without react-ko (pure React): ```tsx <input value={value} onChange={(e) => setValue(e.target.value)} style={{ color }} /> ``` With react-ko: ```tsx <input data-bind="value: value, style: { color: color }" /> ``` No need to wire events or manage local state. Let Knockout observables do the work — even in modern React. --- ## šŸ“ Folder Structure ``` src/ ā”œā”€ā”€ components/ // Components │ ā”œā”€ā”€ scope/ // Core components for binding with Knockout │ │ ā”œā”€ā”€ KnockoutScope.tsx // Manages the Knockout and React scope │ │ └── RootKnockoutProvider.tsx // Root component, initializes Knockout │ ā”œā”€ā”€ structural/ // Generic components for Knockout flow control │ │ ā”œā”€ā”€ KoIf.tsx // ko if: control component │ │ ā”œā”€ā”€ KoIfNot.tsx // ko ifnot: control component │ │ ā”œā”€ā”€ KoForeach.tsx // ko foreach: control component ā”œā”€ā”€ context/ // Context management │ ā”œā”€ā”€ AppViewModelContext.ts // Context related to Knockout's ViewModel ``` --- ## šŸ›  Development ```bash npm install npm run build ``` --- ## šŸ“„ License MIT