You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+40-35Lines changed: 40 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,11 +4,11 @@
4
4
npm install @retsam/ko-react
5
5
```
6
6
7
-
A library for allowing Knockout observables to be used with React components. Knockout's observable system is very similar to MobX, so in practice this is very much like using `mobx-react`.
7
+
A library for allowing Knockout observables to be used with React components. Knockout's observable system is very similar to MobX, so in practice this is very much like using `mobx-react`.
8
8
9
-
This intended as a migration path for legacy Knockout codebases - the knockout html template engine can be replaced with React templates, while leaving the core Knockout logic intact, allowing for an incremental migration path to React.
9
+
This intended as a migration path for legacy Knockout codebases - the knockout html template engine can be replaced with React templates, while leaving the core Knockout logic intact, allowing for an incremental migration path to React.
10
10
11
-
This library provides utilities for allowing React components to rerender, driven by observables (like MobX), and a bindingHandler to bridge from ko templates to react components. There is preliminary support for the reverse - react components to knockout logic - in the form of the `useKnockoutBindings` hook.
11
+
This library provides utilities for allowing React components to rerender, driven by observables (like MobX), and a bindingHandler to bridge from ko templates to react components. There is preliminary support for the reverse - react components to knockout logic - in the form of the `useKnockoutBindings` hook.
12
12
13
13
## API
14
14
@@ -20,16 +20,17 @@ A hook version of `ko.pureComputed`, wraps a function, returns the value of eval
20
20
21
21
```tsx
22
22
interfaceFullNameProps {
23
-
firstName:KnockoutObservable<string>,
24
-
lastName:KnockoutObservable<string>
23
+
firstName:KnockoutObservable<string>;
24
+
lastName:KnockoutObservable<string>;
25
25
}
26
26
27
27
// Re-renders if either firstName or lastName change
Sets up a subscription to an observable (or any subscribable) - runs the provided callback whenever the observable emits a new value, without triggering a rerender (unless the callback modifies state). Disposes the subscription when the component is unmounted.
61
+
Sets up a subscription to an observable (or any subscribable) - runs the provided callback whenever the observable emits a new value, without triggering a rerender (unless the callback modifies state). Disposes the subscription when the component is unmounted.
61
62
62
63
```ts
63
64
typePageTitleComponentProps= {
64
-
text:KnockoutObservable<string>,
65
-
prefix:string,
66
-
}
65
+
text:KnockoutObservable<string>;
66
+
prefix:string;
67
+
};
67
68
const PageTitleComponent = ({}) => {
68
69
const [count, setCount] =useState(0);
69
70
70
71
useSubscription(text, newText=> {
71
72
// count will always be the latest value, no need for a `deps` array.
// Ref of the element where knockout bindings will be applied
123
128
<divref={elementRef}>
124
-
// Standard knockout data-binding
125
-
Hello, <spandata-bind="text: name" />
129
+
// Standard knockout data-binding Hello,{""}
130
+
<spandata-bind="text: name" />
126
131
</div>
127
132
);
128
133
};
129
134
```
130
135
131
136
âš Caveats:
132
137
133
-
* This hook assumes that the ref is stable: if the ref is pointed from one element to a different the bindings won't be reapplied to the new element.
138
+
- This hook assumes that the ref is stable: if the ref is pointed from one element to a different the bindings won't be reapplied to the new element.
134
139
135
-
*Currently no mechanism for setting knockout context, in a Knockout -> React -> Knockout situation, the inner knockout tree won't have access to the outer knockout tree's context. Consider applying the `let` binding if this is necessary.
140
+
-Currently no mechanism for setting knockout context, in a Knockout -> React -> Knockout situation, the inner knockout tree won't have access to the outer knockout tree's context. Consider applying the `let` binding if this is necessary.
136
141
137
-
* There's some danger here about React and Knockout both trying to control the same elements: it's likely safest to not use this hook directly, but to use the provided `KnockoutTemplate` component, which wraps this hook to provide a React version of the template bindingHandler.
142
+
- There's some danger here about React and Knockout both trying to control the same elements: it's likely safest to not use this hook directly, but to use the provided `KnockoutTemplate` component, which wraps this hook to provide a React version of the template bindingHandler.
138
143
139
144
#### KnockoutTemplate
140
145
141
-
A React component which takes a knockout template and data as props, and renders that template inside a <div>. Currently the safest way to host knockout content inside a React tree.
146
+
A React component which takes a knockout template and data as props, and renders that template inside a <div>. Currently the safest way to host knockout content inside a React tree.
0 commit comments