npm install --save react-knockout
Live demo: https://vaaralav.github.io/react-knockout
Source code: example/src
Sandbox: https://codesandbox.io/s/github/vaaralav/react-knockout/tree/master/example
import React, { Component } from 'react';
import counter from './counter'; // ko.observable
import { KoSubscribe } from 'react-knockout';
class Example extends Component {
render() {
return (
<KoSubscribe
subscribe={{
counter,
}}
render={({ counter }) => <pre>{counter}</pre>}
/>
);
}
}
import React, { Component } from 'react';
import counter from './counter'; // ko.observable
import status from './status'; // ko.observable
import queue from './queue'; // ko.observable
import {
KoProvider,
ConnectedKoSubscribe,
withKoSubscribe,
} from 'react-knockout';
function Status({ state: { status = 'Unknown', queue = [] } }) {
return (
<div>
<h3>{status}</h3>
<ol>{queue.map(val => <li>{val}</li>)}</ol>
</div>
);
}
const ConnectedStatus = withKoSubscribe(Status);
class Example extends Component {
render() {
return (
<KoProvider
subscribe={{
counter,
status,
queue,
}}
>
<div>
<ConnectedKoSubscribe
render={({ counter }) => <pre>{counter}</pre>}
/>
<ConnectedStatus />
</div>
</KoProvider>
);
}
}
Makes subscribed ko.observable changes to call the render function provided as render
or children
prop.
subscribe
(Object of ko.observables): The ko.observables you want to subscribe.render
orchildren
(Function): A function that gets the values of the subscribed observables as the first parameter and returns JSX.
Using function as children
.
ReactDOM.render(
<KoSubscribe
subscribe={{
greeting: ko.observable('Hello'),
name: ko.observable('world'),
}}
>
{({ greeting, name }) => `${greeting}, ${name}!`}
</KoSubscribe>,
element,
);
Using render
prop.
ReactDOM.render(
<KoSubscribe
subscribe={{
greeting: ko.observable('Hello'),
name: ko.observable('world'),
}}
render={({ greeting, name }) => `${greeting}, ${name}!`}
/>,
element,
);
Makes the subscribed ko.observables available to withKoSubscribe
and <ConnectedKoSubscribe>
in the component hierarchy.
subscribe
(Object of ko.observables): The ko.observables you want to subscribe.children
(ReactElement): The root of your component hierarchy.
ReactDOM.render(
<KoProvider subscribe={subscriptions}>
<MyRootComponent />
</KoProvider>,
element,
);
A connected component that gives access to observables subscribed with <KoProvider>
above in the component hierarchy.
render
orchildren
(Function): A function that gets the values of the subscribed observables as the first parameter and returns JSX.
ReactDOM.render(
<KoProvider
subscribe={{
greeting: ko.observable('Hello'),
name: ko.observable('world'),
}}
>
<ConnectedKoSubscribe>
{({ greeting, name }) => `${greeting}, ${name}!`}
</ConnectedKoSubscribe>
</KoProvider>,
element,
);
A higher-order component that gives access to observables subscribed with <KoProvider>
above in the component hierarchy.
Component
(ReactComponent): A component that will receive the values of subscribed observables asstate
prop.
const Greeter = ({ state }) => `${state.greeting}, ${state.name}!`;
const ConnectedGreeter = withKoSubscribe(Greeter);
ReactDOM.render(
<KoProvider
subscribe={{
greeting: ko.observable('Hello'),
name: ko.observable('world'),
}}
>
<ConnectedGreeter />
</KoProvider>,
element,
);
MIT © Ville Vaarala