React Day one

JSX

JSX is a react syntax that combines common high-level programming language syntax with html syntax: const element =

Hello, world!

, and it can also be treated as a expression to be returned.

Compile principle

JSX forms:

const element = (
        

Hello, world!

);
const element = React.createElement(
    'h1',
    {className: 'greeting'}, 
    'Hello, world!'
);```

Babel compiles JSX to `React.createElement()` and it  creates an object like this:

const element = {
type: 'hi',
props: {
className: 'greeting',
children: 'Hello, world'
}
};```

Elements & Rendering

  • Elements are what components are "made of".
  • To reander a React element into a root DOM node, pass both to ReactDOM.render():
ReactDOM.render(
    element,
    document.getElementById('root')
);
  • Elements are immutable.
  • React DOM compares the element and its children to the previous one, and only applies the DOM updates necessary to bring the DOM to the desired state.

Components and Props

  • Method one(JavaScript function):
function Welcome(props) {
    return 

Hello, {props.name}

; }
  • Method two(ES6 class):
class Welcome extends React.Component { 
    render() { 
        return  

Hello, {this.props.name}

; } }
  • Elements can also represent user-defined components ( call the function, followed by the parameters): const element = . If the component takes no parameters, than we can directly call the component inside the React render.
  • Components must return a single root element.
  • All React conponents must act like pure function s with respect to their props.

你可能感兴趣的:(React Day one)