๐ง
37์ผ์ฐจ
Part 10. React ๋ก ์ผํ๋ชฐ ๋ง๋ค๊ธฐ (React ๊ธฐ๋ณธ)
Ch 6. ๋ฆฌ์กํธ ์ค์ ํ์ฉ
Ch 6. ๋ฆฌ์กํธ ์ค์ ํ์ฉ
๐ HOC (Higher Order Component) : <์ปดํฌ๋ํธ> ๋ฅผ ์ธ์๋ก ๋ฐ์ <์๋ก์ด ์ปดํฌ๋ํธ> ๋ฅผ ๋ฆฌํดํ๋ ํจ์
https://ko.reactjs.org/docs/higher-order-components.html
https://github.com/mridgway/hoist-non-react-statics
HOC = function(์ปดํฌ๋ํธ) { return ์๋ก์ด ์ปดํฌ๋ํธ; }
- connect()
- createFragmentContainer()
- withRouter()
๐ Controlled Component ์ Uncontrolled Component
- Controlled Component
https://ko.reactjs.org/docs/forms.html#controlled-components
- Uncontrolled Component
https://ko.reactjs.org/docs/uncontrolled-components.html
// ./App.js
import logo from "./logo.svg";
import "./App.css";
import ControlledComponent from "./components/ControlledComponent";
import UncontrolledComponent from "./components/UncontrolledComponent";
function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<ControlledComponent />
<UncontrolledComponent />
</header>
</div>
);
}
export default App;
// ./components/ControlledComponent.jsx
import React from "react";
class ControlledComponent extends React.Component {
state = {
value: "",
};
render() {
const { value } = this.state;
return (
<div>
<input value={value} onChange={this.change} />
<button onClick={this.click}>์ ์ก</button>
</div>
);
}
change = (e) => {
console.log(e.target.value);
this.setState({ value: e.target.value });
};
click = () => {
console.log(this.state.value);
};
}
export default ControlledComponent;
// UncotrolledComponents.jsx
import React from "react";
class UncontrolledComponent extends React.Component {
inputRef = React.createRef();
render() {
console.log("initial render", this.inputRef);
return (
<div>
<input ref={this.inputRef} />
<button onClick={this.click}>์ ์ก</button>
</div>
);
}
componentDidMount() {
console.log("componentDidMount", this.inputRef);
}
click = () => {
// input ์จ๋ฆฌ๋จผํธ์ ํ์ฌ ์ํ ๊ฐ(value) ์ ๊บผ๋ด์ ์ ์กํ๋ค.
// const input = document.querySelector("#my-input");
// console.log(input.value);
console.log(this.inputRef.current.value);
};
}
export default UncontrolledComponent;