Mohammed Erraysy

  • Frontend Developer @SanadTech
  • Self Taught
  • Basketball Player
  • Gamer

@merraysy

merraysy@gmail.com

React

Title

2010: XHP

Facebook introduced XHP into it's PHP stack and open sourced it.

<?hh

require __DIR__ . "/vendor/hh_autoload.php";

class MyClass {
  public function getInt(): int {
    return 4;
  }
}

function get_string(): string {
  return "Hello";
}

function get_float(): float {
  return 1.2;
}

function embed_hack(): void {
  $xhp_float = <i>{get_float()}</i>;
  $a = new MyBasicUsageExampleClass();

  echo (
    <div>
      {(new MyClass())->getInt()}
      <strong>{get_string()}</strong>
      {$xhp_float}
    </div>
  );
}

basic_usage_examples_embed_hack();

2011: FaxJS

Jordan Walke created FaxJS, the early prototype of React.

2013: React

React gets open sourced.

Problems

  • String concatenation
  • XSS (Cross-Site Scripting) Attacks
  • DOM manipulation

String Concatenation

// string concatenation
<?hh
$user_name = 'Fred';
echo "<tt>Hello <strong>$user_name</strong></tt>";
// string concatenation
var element = '<div class="' + containerClass + ">'
    + '<img src="' + imgSrc + '" />'
    + '</div>';

DOM Manipulation

// Bad
$('.btn').click(function (event) {
    var $target = $('.target');
    var newValue = $target.value() + $('.source').value();

    $target.html(newValue);

    emitter('target-changed', { value: newValue }).then(function (ok) {
        if (ok && $('.related-source').value() === 'yes deleted it') {
            $('.to-be-deleted').remove();
        }
    });
});
// Good
$('.btn').click(function (event) {
    var $target = $('.target');
    var newValue = $target.value() + $('.source').value();
    var relatedValue = computeRelatedValue(newValue);

    emitter('target-changed', { value: newValue }).then(function (ok) {
        if (ok && relatedValue === 'yes deleted it') {
            $target.html(newValue);
            $('.to-be-deleted').remove();
        }
    });
});

Read

Write

Read

Write

Read

Read

Write

Write

Solutions

XHP => JSX

// string concatenation
<?hh
$user_name = 'Fred';
echo "<tt>Hello <strong>$user_name</strong></tt>";
// XHP
<?hh
$user_name = 'Fred';
echo <tt>Hello <strong>{$user_name}</strong></tt>;
// JSX
const button = <button className="btn">Call to action</button>;
// JSX output
const button = React.createElement(
    'button',
    { className: 'btn' },
    'Call to action',
);

Virtual DOM

Let's take a look

Philosophy

Declarative

function formatTime(time) {
    moment(time).format(timeFormat);
}

function Component(props) {
    return (
        <Clock
            time={formatTime(props.time)}
            onReset={props.resetClock}
        />
    );
}

Component Based

class Component {
    render() {
        const {
            data,
            changeData,
        } = this.props;

        return (
            <App>
                <Header title={data.title} />
                <Grid rows={data.rows} />
                <Input onChange={changeData} />
            </App>
        );
    }
}

Show me the CODE!

JSX

Render Elements

Components

  • Props
  • State

Events

Lists and Keys

Forms

  • Uncontrolled
  • Controlled

Composition

  • Containment
  • Specialization

Thank

You
All

React

By Mohammed Erraysy

React

An introduction to the React library

  • 132