JS Lover
JS Lover

Reputation: 622

what does "event =&gt" does in RxJS?

I came across this code below but I don't understand why we are doing event is equal and greater than and then console.log I would really appreciate if someone explains it

const node = document.querySelector('input[type=text]');

const input$ = Rx.Observable.fromEvent(node, 'input');

input$.subscribe({
  next: event => console.log(`You just typed ${event.target.value}!`),
  error: err => console.log(`Oops... ${err}`),
  complete: () => console.log(`Complete!`),
});

const input$ = Rx.Observable.fromEvent(node, 'input')
  .map(event => event.target.value)
  .filter(value => value.length >= 2)
  .subscribe(value => {
    // use the `value`
  });

Upvotes: 0

Views: 109

Answers (2)

Nino Filiu
Nino Filiu

Reputation: 18513

This is invalid JavaScript. =&gt should be => and what you're seeing is simply ES6's arrow functions.

This is a display bug in the page you're seeing. It probably stems from the fact that < and > in HTML text should be transformed into &lt; and &gt; so as not to cause parse errors with the same characters when they serve as tag opening and closing (as in <div>).

Upvotes: 1

Etheryte
Etheryte

Reputation: 25319

This looks like Javascript code that's been passed through an HTML sanitizer.

The original code using arrow functions should be as follows:

const node = document.querySelector('input[type=text]');

const input$ = Rx.Observable.fromEvent(node, 'input');

input$.subscribe({
  next: event => console.log(`You just typed ${event.target.value}!`),
  error: err => console.log(`Oops... ${err}`),
  complete: () => console.log(`Complete!`),
});

const input$ = Rx.Observable.fromEvent(node, 'input')
  .map(event => event.target.value)
  .filter(value => value.length >= 2)
  .subscribe(value => {
    // use the `value`
  });

Upvotes: 2

Related Questions