「React入門」React.Component継承のサンプル

書式
class クラス名 extends React.Component
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Hello React!</title>
<script src="/react/16.4.0/umd/react.development.js"></script>
<script src="/react-dom/16.4.0/umd/react-dom.development.js"></script>
<script src="/babel-standalone/6.26.0/babel.min.js"></script>
</head>
<body>
<div id="cft"></div>
<script type="text/babel">
function FormattedDate(props) {
return <h2>時刻 {props.date.toLocaleTimeString()}.</h2>;
}
class Clock extends React.Component {
constructor(props) {
super(props);
this.state = {date: new Date()};
}
componentDidMount() {
this.timerID = setInterval(
() => this.tick(),
1000
);
}
componentWillUnmount() {
clearInterval(this.timerID);
}
tick() {
this.setState({
date: new Date()
});
}
render() {
return (
<div>
<h1>Hello, world!study skill in arkgame</h1>
<FormattedDate date={this.state.date} />
</div>
);
}
}
ReactDOM.render(
<Clock />,
document.getElementById('cft')
);
</script>
</body>
</html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Hello React!</title> <script src="/react/16.4.0/umd/react.development.js"></script> <script src="/react-dom/16.4.0/umd/react-dom.development.js"></script> <script src="/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="cft"></div> <script type="text/babel"> function FormattedDate(props) { return <h2>時刻 {props.date.toLocaleTimeString()}.</h2>; } class Clock extends React.Component { constructor(props) { super(props); this.state = {date: new Date()}; } componentDidMount() { this.timerID = setInterval( () => this.tick(), 1000 ); } componentWillUnmount() { clearInterval(this.timerID); } tick() { this.setState({ date: new Date() }); } render() { return ( <div> <h1>Hello, world!study skill in arkgame</h1> <FormattedDate date={this.state.date} /> </div> ); } } ReactDOM.render( <Clock />, document.getElementById('cft') ); </script> </body> </html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Hello React!</title>
<script src="/react/16.4.0/umd/react.development.js"></script>
<script src="/react-dom/16.4.0/umd/react-dom.development.js"></script>
<script src="/babel-standalone/6.26.0/babel.min.js"></script>
</head>
<body>

<div id="cft"></div>
<script type="text/babel">
function FormattedDate(props) {
  return <h2>時刻 {props.date.toLocaleTimeString()}.</h2>;
}

class Clock extends React.Component {
  constructor(props) {
    super(props);
    this.state = {date: new Date()};
  }

  componentDidMount() {
    this.timerID = setInterval(
      () => this.tick(),
      1000
    );
  }

  componentWillUnmount() {
    clearInterval(this.timerID);
  }

  tick() {
    this.setState({
      date: new Date()
    });
  }

  render() {
    return (
      <div>
        <h1>Hello, world!study skill in arkgame</h1>
        <FormattedDate date={this.state.date} />
      </div>
    );
  }
}
ReactDOM.render(
  <Clock />,
  document.getElementById('cft')
);
</script>

</body>
</html>

 

React.js

Posted by arkgame