React in 15 min

by Javi Perez

12/2015

What is React?

Javascript library for building user interfaces

What is React useful for?

  • Create visual components
  • Handle your UI in a modular way
  • Work with abstraction from the DOM
  • Import and export components
  • Test the UI in a modular way
  • Render visual components in a functional way
  • Optimize render DOM changes
  • Server rendering

hold on there

show me some code

class MyComponent extends React.Component {
  render () {
    return <p />;
  }
}

Create a React Component

ReactDOM.render(
  <MyComponent />,
  document.getElementById('MyComponent')
);

Bind a DOM element with the component

My first React component

class Avatar extends React.Component {
  render () {
    return (
      <div className="avatar" onClick={this.props.onClick}>
        <img src={this.props.avatar} alt="avatar" />
        <span>{this.props.username}</span>
      </div>
    );
  }
}

Avatar.propTypes = {
  avatar: React.PropTypes.string.isRequired,
  username: React.PropTypes.string.isRequired,
  onClick: React.PropTypes.func
};

Avatar.defaultProps = {
  onClick: () => {}
};
var props = {
  avatar: "//avatar.com/defaultAvatar.png",
  username: "javi",
  onClick: () => {
    console.log("the avatar has been clicked");
  }
}

ReactDOM.render(<Avatar {...props}/>, document.getElementById("MyComponent"));

Bind the component

<div className="avatar">
  <img src="//avatar.com/defaultAvatar.png" alt="avatar" />
  <span>javi</span>
</div>

It will render

var props = {
  avatar: "//avatar.com/defaultAvatar.png",
  username: "javi",
  onClick: () => {
    console.log("the avatar has been clicked");
  }
}

ReactDOM.render(<Avatar {...props}/>, document.getElementById("MyComponent"));

Avatar.setProps({
  username: "belu"
});

How to update the properties

<div className="avatar">
  <img src="//avatar.com/defaultAvatar.png" alt="avatar" />
  <span>belu</span>
</div>

After set the new username It will render

Thinking in React

Great!

show me the app

How to Integrate React

  • MVC with React

  • Flux with React

MVC with React

Flux with React

who is using react?

Want to know more?

Keep in touch and ask for help

Recommended links

react-in-15-min

By Javier Perez

react-in-15-min

  • 655