-
Notifications
You must be signed in to change notification settings - Fork 1
Example suggestion with React usage #7
Comments
Awesome! Thanks for taking an interesting in the project. Was there anything you found confusing about the examples currently in the README? Especially re: not being clear that I'll add this as an example to the README soon. Or you can open a pull request of course! :) |
Yeah, I can probably put out a PR this weekend. Do you have any comments on my code though? I'm pretty new to RxJS, so I'm not sure if I'm doing things quite the right way yet. |
From a code correctness perspective, there are two things that I see:
|
|
I edited/updated my comment with more points before I saw your reply. I'll post them later. Clearly I misread/misremembered your original post, I'm sorry! My second point is totally wrong, you are unsubscribing correctly. When you create Subscribing twice, in general, is fine though. Usually I only use |
It seems to work without the |
I'm actually also doubting if I'm unsubscribing properly now. I think I might need to save the subscription object from calling |
That's correct! I missed that. Yes, you need to keep a reference to what is returned by |
Revised example: import React from 'react';
import { observeComponent, fromComponent } from 'observe-component/rxjs';
// Create the component with the listeners we want
const TextArea = observeComponent('onInput')('textarea');
export default class MyComponent extends React.Component {
constructor() {
super();
this.state = { typing: false };
}
componentDidMount() {
// Create stream for when user is typing
const inputEvents$ = fromComponent(TextArea, 'onInput');
// Create stream for when user has been idle for 1 second
const idle$ = inputEvents$.debounceTime(1000);
// Subscribe to invoke the streams and save the subscriptions
this.inputEventsSub = inputEvents$.subscribe(() => this.setState({ typing: true }));
this.idleSub = idle$.subscribe(() => this.setState({ typing: false }));
}
componentWillUnmount() {
// Unsubscribe from the streams when component unmounts
this.inputEventsSub.unsubscribe();
this.idleSub.unsubscribe();
}
render() {
return (
<div>
<TextArea cols="30" rows="10" />
<div>{this.state.typing ? 'User is typing...' : 'User is idle'}</div>
</div>
);
}
} |
I had a little bit of trouble getting started in the beginning. I didn't realize that
fromComponent
andobserveComponent
were both needed. In any case, I've created the following example for other people who are also using React and RxJS v5 with this library.@Lokeh feel free to add this example to your docs, and thanks for the library! Please let me know if there can be any improvements to the following code.
The text was updated successfully, but these errors were encountered: