Created
August 7, 2020 12:29
-
-
Save kuredev/717eabece4b34dd922350a75023b74cc to your computer and use it in GitHub Desktop.
フィボナッチ数列を計算するReactコンポーネント
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>Fibonacci</title> | |
<script src="https://unpkg.com/react@16/umd/react.development.js"></script> | |
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> | |
<script src="https://unpkg.com/[email protected]/babel.js"></script> | |
</head> | |
<body> | |
<div id="root">wait...</div> | |
<script type="text/babel"> | |
// State をクリックで更新する | |
class Fibonacci extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
num1: 1, | |
num2: 1, | |
msg: "Fibonacci: 1, 1" | |
}; | |
this.calcNext = this.calcNext.bind(this); | |
} | |
calcNext(e) { | |
this.setState( | |
(state) => { | |
return { | |
num2: state.num1, | |
num1: state.num1 + state.num2, | |
msg: state.msg + ", " + (state.num1 + state.num2) | |
} | |
} | |
) | |
} | |
render() { | |
return <div> | |
<p>{this.state.msg}</p> | |
<button onClick={this.calcNext}>Calc</button> | |
</div>; | |
} | |
} | |
ReactDOM.render(<Fibonacci />, document.getElementById('root')); | |
</script> | |
</body> | |
</html> |
Author
kuredev
commented
Aug 7, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment