teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

コードの追加

2020/03/30 22:45

投稿

退会済みユーザー
answer CHANGED
@@ -1,1 +1,60 @@
1
- Organisms側から関数などを渡す。
1
+ Organisms側から関数などを渡す。(最適かどうかは別として、最低限の動作はする)
2
+
3
+ ```JavaScript
4
+ import React, { Component } from 'react';
5
+
6
+ import Counter from './atoms/Counter';
7
+ import Btn from './atoms/Btn';
8
+
9
+ export default class Organisms extends Component {
10
+ constructor(props) {
11
+ super(props);
12
+ this.handleClick = this.handleClick.bind(this);
13
+ this.state = {
14
+ count: 0,
15
+ }
16
+ }
17
+
18
+ handleClick() {
19
+ this.setState({
20
+ count: this.state.count + 1,
21
+ })
22
+ }
23
+
24
+ render()
25
+ {
26
+ return(
27
+ <>
28
+ <Counter count={this.state.count} onClick={this.handleClick}/>
29
+ <Btn text="Click!" onClick={this.handleClick}/>
30
+ </>
31
+ );
32
+ }
33
+ }
34
+
35
+ ```
36
+
37
+ ```JavaScript
38
+ import React, { Component } from 'react';
39
+
40
+ const Btn = (props) => {
41
+ return(
42
+ <button onClick={props.onClick}>{props.text}</button>
43
+ );
44
+ }
45
+
46
+ export default Btn;
47
+
48
+ ```
49
+ ```JavaScript
50
+ import React, { Component } from 'react';
51
+
52
+ const Counter = (props) => {
53
+ return(
54
+ <h3>{props.count}</h3>
55
+ );
56
+ }
57
+
58
+ export default Counter;
59
+
60
+ ```