Skip to content

Commit f136ee6

Browse files
committed
Use prettier
1 parent 0e0da6c commit f136ee6

26 files changed

Lines changed: 411 additions & 369 deletions

.prettierignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package.json
2+
package-lock.json
3+
dist
4+
.vscode

.prettierrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"trailingComma": "all",
3+
"tabWidth": 4,
4+
"printWidth": 80,
5+
"endOfLine": "lf",
6+
"arrowParens": "avoid"
7+
}

README.md

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
npm install @retsam/ko-react
55
```
66

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`.
88

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.
1010

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.
1212

1313
## API
1414

@@ -20,16 +20,17 @@ A hook version of `ko.pureComputed`, wraps a function, returns the value of eval
2020

2121
```tsx
2222
interface FullNameProps {
23-
firstName: KnockoutObservable<string>,
24-
lastName: KnockoutObservable<string>
23+
firstName: KnockoutObservable<string>;
24+
lastName: KnockoutObservable<string>;
2525
}
2626

2727
// Re-renders if either firstName or lastName change
28-
const Greeter = ({firstName, lastName}: FullNameProps) => useComputed(() => (
29-
<span>
30-
Hello, {firstName()} {lastName()}
31-
</span>
32-
));
28+
const Greeter = ({ firstName, lastName }: FullNameProps) =>
29+
useComputed(() => (
30+
<span>
31+
Hello, {firstName()} {lastName()}
32+
</span>
33+
));
3334
```
3435

3536
Can largely be used as a drop-in replacement for the `observe` HOC.
@@ -40,53 +41,57 @@ Reads and subscribes to an observable - if the observable's value changes the co
4041

4142
```tsx
4243
interface FullNameProps {
43-
firstName: KnockoutObservable<string>,
44-
lastName: KnockoutObservable<string>
44+
firstName: KnockoutObservable<string>;
45+
lastName: KnockoutObservable<string>;
4546
}
4647

4748
// Re-renders only if firstName changes
48-
const Greeter = ({firstName, lastName}: FullNameProps) => {
49-
const fName = useObservable(firstName)
49+
const Greeter = ({ firstName, lastName }: FullNameProps) => {
50+
const fName = useObservable(firstName);
5051
return (
5152
<span>
5253
Hello, {fName} {lastName()}
5354
</span>
5455
);
55-
}
56+
};
5657
```
5758

5859
#### `useSubscription`
5960

60-
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.
6162

6263
```ts
6364
type PageTitleComponentProps = {
64-
text: KnockoutObservable<string>,
65-
prefix: string,
66-
}
65+
text: KnockoutObservable<string>;
66+
prefix: string;
67+
};
6768
const PageTitleComponent = ({}) => {
6869
const [count, setCount] = useState(0);
6970

7071
useSubscription(text, newText => {
7172
// count will always be the latest value, no need for a `deps` array.
72-
document.title = `${count} - ${newText}`
73+
document.title = `${count} - ${newText}`;
7374
});
7475

7576
return <button onClick={() => setCount(count + 1)}>Click</button>;
76-
}
77+
};
7778
```
7879

7980
### Knockout bindingHandler `reactComponent`
8081

8182
Used to host a react tree inside a Knockout app, useful for incrementally migrating from knockout templates to React components.
8283

8384
```html
84-
<div data-bind="
85+
<div
86+
data-bind="
8587
reactComponent: {
8688
Component: MyComponent,
8789
props: {prop: 'propValue'}
8890
}
89-
"><!-- MyComponent will render here --></div>
91+
"
92+
>
93+
<!-- MyComponent will render here -->
94+
</div>
9095
```
9196

9297
Must be registered in `ko.bindingHandlers`, can be done by calling the exported `register` function.
@@ -112,7 +117,7 @@ While the majority of this library is aimed at hosting React trees inside of Kno
112117
This hook takes an element ref and a set of knockout bindings and applies those bindings to the element.
113118

114119
```tsx
115-
const MessageComponent = ({text}: {text: string}) => {
120+
const MessageComponent = ({ text }: { text: string }) => {
116121
const elementRef = useRef<HTMLDivElement>(null);
117122

118123
const viewModel = { name: text };
@@ -121,30 +126,30 @@ const MessageComponent = ({text}: {text: string}) => {
121126
return (
122127
// Ref of the element where knockout bindings will be applied
123128
<div ref={elementRef}>
124-
// Standard knockout data-binding
125-
Hello, <span data-bind="text: name" />
129+
// Standard knockout data-binding Hello,{" "}
130+
<span data-bind="text: name" />
126131
</div>
127132
);
128133
};
129134
```
130135

131136
âš  Caveats:
132137

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.
134139

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.
136141

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.
138143

139144
#### KnockoutTemplate
140145

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.
142147

143148
```tsx
144-
const KnockoutGreeter = ({firstName, lastName}) => (
149+
const KnockoutGreeter = ({ firstName, lastName }) => (
145150
<KnockoutTemplate
146151
name="knockoutGreeterTemplate"
147-
data={{firstName, lastName}}
152+
data={{ firstName, lastName }}
148153
/>
149154
);
150155
```
@@ -157,12 +162,12 @@ A Higher Order Component which wraps a component such that any observables that
157162

158163
```tsx
159164
interface FullNameProps {
160-
firstName: KnockoutObservable<string>,
161-
lastName: KnockoutObservable<string>
165+
firstName: KnockoutObservable<string>;
166+
lastName: KnockoutObservable<string>;
162167
}
163168

164169
// Re-renders if either firstName or lastName change
165-
const Greeter = observe(({firstName, lastName}: FullNameProps) => (
170+
const Greeter = observe(({ firstName, lastName }: FullNameProps) => (
166171
<span>
167172
Hello, {firstName()} {lastName()}
168173
</span>

0 commit comments

Comments
 (0)