Project WebApp

From code to UI - Introduction to React.JS

April 24, 2018

to use react we need two packages from npm, react itself and react-dom. React takes care of managing a virtual dom representation and is highly optimized to handle state updates and changes in the virtual dom tree following the state change. The second part “react-dom” is responsible for bringing the virtual representation of the dom to the browser and also provides wrapped APIs for browser based stuff like event-handlers on elements.

Because every new beginning starts with a hello-world, here it is in react:

ReactDOM.render(
  <h1>Hello, world!</h1>,
  document.getElementById("root"),
);

As you can see there is some html-like syntax mixed right into the JavaScript-Code, this language extension is called JSX. This extended syntax enables you to write shorter and more readable code, but it must be transpiled to real JavaScript before it can be executed in a browser.

The hello-world as transpiled code looks like this:

ReactDOM.render(
  React.createElement("h1", null, "Hello, world!"),
  document.getElementById("root"),
);

So every JSX-Tag gets replaced with a call to React.createElement(tag, props, children). That’s already a nice to have feature, but JSX starts to shine when you define your own UI components and can use them as new JSX-tags. Components can either be classes extending the React.Component class, or simple functions returning valid JSX elements. There is only one convention based rule, the name of the Component either class or function must start with a capital letter, because all JSX-tags starting with a lowercase letter are interpreted as native browser elements. Now a hello-world with a self defined component:

const HelloWorld = () => <h1>Hello, world!</h1>;
ReactDOM.render(
  <HelloWorld />,
  document.getElementById("rooot"),
);

—>

var HelloWorld = function HelloWorld() {
  return React.createElement("h1", null, "Hello, world!");
};
ReactDOM.render(
  React.createElement(HelloWorld, null),
  document.getElementById("root"),
);

For a better understanding of the JSX syntax and its corresponding JavaScript code the babel REPL is a great tool tp play with.

The HelloWorld component didn’t have any parameters but as part as you can see in the React.createElement, there is second parameter null. If you want to provide some information to a component you can define and use the properties like this:

const HelloName = ({ name }) => <h1>Hello, {name}!</h1>;
ReactDOM.render(
  <HelloName name={"Peter"} />,
  document.getElementById("root"),
);

—>

var HelloName = function HelloName(props) {
  return React.createElement(
    "h1",
    null,
    "Hello, ",
    props.name,
    "!",
  );
};
ReactDOM.render(
  React.createElement(HelloName, { name: "Peter" }),
  document.getElementById("root"),
);

The same component defined as a Component-class looks like this:

class HelloName extends React.Component {
  render() {
    const { name } = this.props;
    return <h1>Hello, {name}!</h1>;
  }
}
ReactDOM.render(
  <HelloName name={"Peter"} />,
  document.getElementById("root"),
);

But Component-classes are only necessary if you have to manage some state inside of the component, and even this can be done with components providing state-management functions.

The last important bit of information about the JSX syntax is the children property. With children you get access to the information given to you between the opening and closing tag of your component. So you could redefine the HelloName component like this:

const HelloName = ({ children }) => (
  <h1>Hello, {children}!</h1>
);
ReactDOM.render(
  <HelloName>Peter</HelloName>,
  document.getElementById("root"),
);
// behaves the same as
ReactDOM.render(
  <HelloName children={"Peter"} />,
  document.getElementById("root"),
);

But with children you provide other JSX-elements so you could also write:

const HelloName = ({ children }) => (
  <h1>Hello, {children}!</h1>
);
render(
  <HelloName>
    Peter <h2>some nested stuff</h2>
  </HelloName>,
);

Hello, Peter

some nested stuff

!

So that’s it. Now you know the most important things about JSX and can start to use react.

Tasks

  • work through the very fine react tutorial
  • when finished with the tic-tac-toe game change the css rules to use flex-box and check if the react-layout code can be simplified

Written by Kalle Ott for opencampus