reactive programming with reactjs programming with...are hard to render on the server. this makes...

69
Reactive programming in JavaScript with Reactjs JFokus 3. february 2015

Upload: nguyennhi

Post on 30-Mar-2018

219 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Reactive  programming  

in JavaScript with Reactjs

JFokus 3. february 2015

Page 2: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Forget  about…

Established truths

Everything you thought you knew about making web apps

Page 3: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Relax

It’s going to be okay

Page 4: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Hello,  I’m  Sven

I am a frontend developer from Inmeta Consulting in Norway

Page 5: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

The  ProblemHow can we build large apps with data

that changes over time?

But: local state that changes over time is the root of all evil

Page 6: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

ModelViewControllerThe MVC pattern was developed in

1979

It was deviced as a general solution to the problem of users controlling a

large and complex data set.

It’s not 1979 anymore…

Page 7: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

The  MVC  problemThin views / templates

Models and controllers that grows…

…and grows

until most of your time is spent keeping them in sync

Page 8: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

We  need  a  better  model

React

A JavaScript library for building composable user interfaces

Page 9: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

React  gives  youA lightweight virtual DOM

Powerful views without templates

Unidirectional data flow

Explicit mutation

Page 10: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

A  React  app  consists  of

Reusable components

Components makes code reuse, testing, and separation of concerns easy.

Page 11: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Not  just  the  VIn the beginning, React was presented

as the V in MVC.

This is at best a huge simplification.

React has state, it handles mapping from input to state changes, and it

renders components. In this sense, it does everything that an MVC does.

Page 12: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can
Page 13: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

<NewsFeed>

Page 14: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

<NewsItem>

Page 15: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

<ItemComments>

<ItemTitle>

<ItemCover>

Page 16: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

NewsItem.jsx

Page 17: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

NewsItem.jsx

Page 18: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

NewsItem.jsx

Page 19: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

NewsItem.jsx

Page 20: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

JSX

A JavaScript XML based extension that makes it easy to mix HTML with

JavaScript

Page 21: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can
Page 22: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Component  Life  Cycle

Initial  render Get  Initial  State

Component  Will  Mount

Render

Component  Did  Mount

Get  Default  Props

Called  immediately  after  render

Set  initial  value  of  this.state

Set  initial  value  of  this.props

Return  JSX  for  component  Never  update  state  here

Calling  setState  here  does    not  cause  a  re-­‐render

Page 23: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Component  Life  Cycle

Component  will  receive  props

Should  component  update

Component  will  update

Render

Component  did  update

-­‐  Takes  nextprops  as  input  -­‐  Previous  props  available    as  this.props  -­‐  Calling  setState()  here  does  not  trigger  re-­‐render

Can  abort  render  if  you  return  false  here.  If  false,  componentWillUpdate  and    componentDidUpdate  will  not  be  called.nextProps,  nextState  available  here  Cannot  use  setState()  here

Called  immediately  after  render

NOT  called  for    initial  render

PROPS  Change

Page 24: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Component  Life  Cycle

Should  component  update

Component  will  update

Render

Component  did  update

Can  abort  render  if  you  return  false  here.  If  false,  componentWillUpdate  and    componentDidUpdate  will  not  be  called.

nextProps,  nextState  available  here  Cannot  use  setState()  here

Called  immediately  after  render

NOT  called  for    initial  render

STATE  Change

Page 25: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Component  Life  Cycle

These methods do not have access to the component’s props or state

StaticsThe statics object allows you to define static methods that can be invoked on the component without creating instances

Page 26: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Component  Life  Cycle

Component  will  unmountInvoked  immediately  before  component  is  unmounted.  For  cleanup,  invalidating    timers  etc.

Unmount

Page 27: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Virtual  DOMRender    the  DOM

Build  a  new    Virtual  DOM

Compute  the  minimal    sets  of  mutation  and  queue

Diff  with    old  DOM

Batch  execute  all  updates EACH  UPDATE

Page 28: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

State PropsFor  interactivity    in  the  component.    Mutable  data

For  data  passed    to  the  component  Should  be  treated  as    

immutable.

Page 29: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

StateIs  updated  by  calling  setState()

Every  call  to  setState()  triggers  a  re-­‐render  

(except  when  called  within  componentDidMount)

Page 30: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Only  the  changes    are  rendered

Everything  is    re-­‐rendered

React jQuery

Page 31: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Server  Rendering

Traditional JavaScript applications are hard to render on the server. This makes the app uncrawlable, and you

miss out on SEO.

Page 32: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Server  RenderingFortunately, React can handle this

with ease.

All you need to do is call renderToString instead of render

and you’ve got a SEO ready component.

Page 33: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Server  RenderingAnother option is to call

renderToStaticMarkup.

This is similar to renderToString, except this doesn't create extra DOM

attributes such as data-react-id which is useful if you want to use React as a

simple static page generator.

Page 34: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Testing

Page 35: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

JEST

Built on top of the Jasmine test framework, using familiar

expect(value).toBe(other) assertions

Page 36: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

JEST

Automatically finds tests to execute in your repo

Page 37: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

JEST

Automatically mocks dependencies for you when running your tests

Page 38: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

JEST

Allows you to test asynchronous code synchronously

Page 39: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

JEST

Runs your tests with a fake DOM implementation (via jsdom) so that your tests can run on the command

line

Page 40: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

JEST

In short, if you want to test React code, use JEST.

Page 41: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Practical  exampleUnclicked State Clicked State

Page 42: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can
Page 43: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can
Page 44: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can
Page 45: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can
Page 46: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can
Page 47: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can
Page 48: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can
Page 49: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can
Page 50: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can
Page 51: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can
Page 52: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can
Page 53: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can
Page 54: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can
Page 55: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

RoutingReact does not have a native router

There are however a few to choose between

React-routerReact-router-component

Monorouter

Page 56: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

React-­‐router  example

Page 57: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Inline  Styles

Page 58: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

So  inline  styles,  eh?

There’s actually a good reason for doing this.

Page 59: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

So  inline  styles,  eh?

CSS pollutes the global namespace

At scale, this is bad because it leads to paralysis and confusion.

Can I add this element, or change this class? If you’re not sure, you’re in

trouble.

Page 60: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

So  inline  styles,  eh?

Inline styles avoid this, because the CSS is scoped to the component you’re

working with.

Page 61: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

How  it  looks

Page 62: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Not  your  80s  inlineIt's not really "inline". We merely pass a reference to a rule that’s somewhere else in the file, just like

CSS.

Style is actually a much better name than class. You want to “style” the element, not “class” it.

Finally, this is not applying the style directly, this is using React virtual DOM and is being diff-ed the

same way elements are.

Page 63: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Still….

The goal is not to replace CSS as it’s done today. It’s simply focusing on the fundamental problem

with CSS and trying to solve it.

You do not have to use it. If you apply a className tag to your elements, you can use CSS as you’ve

always done.

Page 64: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Mixins

Basically, pure React components that can be incorporated in your other

components

Page 65: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Mixins

Components that use mixins inherits state and props from the mixin

Page 66: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Mixins

Page 67: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Last  words

Virtual DOM, a native event system and other technicalities are nice

But Reacts true strength are actually none of these

Page 68: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Last  wordsReacts true strengths are:

Unidirectional Data Flow

Freedom from Domain Specific Language (it’s all JavaScript)

Explicit Mutation

Page 69: Reactive programming with Reactjs programming with...are hard to render on the server. This makes the app uncrawlable, and you miss out on SEO. ServerRendering Fortunately, React can

Questions?Source  Code  available  at    

github.com/svenanders/react-­‐tutorial  

http://learnreact.robbestad.com