其他分享
首页 > 其他分享> > React.js -- Getting Started

React.js -- Getting Started

作者:互联网

Contents:

  • React is a Javascript library
  • High performance frontend framework
  • Use small and isolated pieces of UI code called “components

Initialize React App

npx create-react-app my-app
cd my-app
npm start

App structure

React.Component

class ShoppingList extends React.Component {
    render(){
        return(
            //...
        )
    }
}

React element

  • React UI的最小单元
  • 不可变:当元素被创建之后,你是无法改变其内容或属性的
  • 可以被render进react DOM,并由其来管理

render()

将React元素render到DOM root

const element = <h1>Hello, world!</h1>;
ReactDOM.render(
    element,
    document.getElementById('example')
);

function封装react element

// function that return JSX.element
function Clock(props) {
  return (
    <div>
      <h1>Hello, world!</h1>
      <h2>现在是 {props.date.toLocaleTimeString()}.</h2>
    </div>
  );
}

// function that renders element
function tick() {
  ReactDOM.render(
    <Clock date={new Date()} />,
    document.getElementById('example')
  );
}

// repeatedly calling the rendering function -- 每秒钟调用一次
setInterval(tick, 1000);

Component封装element

React.component

class Clock extends React.Component {
    render(){
        return(
           ...
           <h2>现在是 {this.props.date.toLocaleTimeString()}.</h2>
           ... 
        )
    }
}

function tick() {
...
}

setInterval(tick, 1000);

标签:function,react,render,Getting,--,element,React,...
来源: https://blog.csdn.net/DOITJT/article/details/122660421