Skip to content

Commit 37f5c43

Browse files
committed
HOC
1 parent e6c5071 commit 37f5c43

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

chapter7-3-1/src/Expandable.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ const Expandable = ComposedComponent =>
2020
{...this.state}
2121
{...this.props} />
2222
}
23-
}
23+
}
24+
25+
export default Expandable;

chapter7-3-1/src/MenuButton.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React, {Component} from 'react';
2+
class MenuButton extends Component {
3+
componentWillReceiveProps(nextProps){
4+
const collapsed = (nextProps.collapsed && nextProps.collapsed === true) ?true : false;
5+
this.setState({collapsed});
6+
}
7+
8+
render() {
9+
const {children, collapsed, txt, expandCollapse} = this.props;
10+
return (
11+
<div className="pop-button">
12+
<button onClick={expandCollapse}>{txt}</button>
13+
{(!collapsed) ? <div className="pop-up">{children}</div> : ''}
14+
</div>
15+
)
16+
}
17+
}
18+
19+
export default MenuButton;

chapter7-3-1/src/index.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
1-
import React from 'react';
1+
import React, {Children} from 'react';
22
import ReactDOM from 'react-dom';
33
import './index.css';
4-
import App from './App';
54
import * as serviceWorker from './serviceWorker';
65
import Expandable from './Expandable';
6+
import MenuButton from './MenuButton';
77

8+
const Letter = /./g
89
const ShowHideMessage = ({children, collapsed, expandCollapse}) =>
910
<p onClick={expandCollapse}>
1011
{(collapsed) ? children.replace(Letter, 'x') : children}
1112
</p>
1213
const HiddenMessage = Expandable(ShowHideMessage);
1314

14-
ReactDOM.render(<HiddenMessage />, document.getElementById('root'));
15+
const PopupButton = Expandable(MenuButton);
16+
17+
ReactDOM.render(
18+
<React.Fragment>
19+
<HiddenMessage >
20+
방가방가
21+
</HiddenMessage>
22+
<PopupButton hidden={true} txt="팝업 토글">
23+
<h1>숨겨진 콘텐츠</h1>
24+
<p>이 콘텐츠는 처음에 숨겨져있습니다.</p>
25+
</PopupButton>
26+
</React.Fragment>, document.getElementById('root'));
1527

1628
// If you want your app to work offline and load faster, you can change
1729
// unregister() to register() below. Note this comes with some pitfalls.

0 commit comments

Comments
 (0)