Props는 부모 컴포넌트로부터 자식 컴포넌트에 데이터를 보내는 어떠한 방식이다.
<script type="text/babel">
function SaveBtn() {
return (
<button
style={{
backgroundColor: "tomato",
color: "white",
padding: "10px 20px",
border: 0,
borderRadius: "10px",
}}
>
Save Changes
</button>
);
}
function ConfirmBtn() {
return <button>Confirm</button>;
}
function App() {
return (
<div>
<SaveBtn />
<ConfirmBtn />
</div>
);
}
const root = document.getElementById("root");
ReactDOM.render(<App />, root);
</script>



banana="--" 같은 형식을 사용해 보았다.

props.banana를 버튼 태그의 텍스트로 사용하라고 지정하면 이렇게 잘 사용이 된다. 

<script type="text/babel">
function Btn({ text, big }) {
console.log("text : " + text + ", big : " + big);
return (
<button
style={{
backgroundColor: "tomato",
color: "white",
padding: "10px 20px",
margin: "3px",
border: 0,
borderRadius: "10px",
fontSize: big ? 18 : 10,
}}
>
{text}
</button>
);
}
function App() {
return (
<div>
<Btn text="Save Changes" big="true" />
<Btn text="Continue" />
</div>
);
}
const root = document.getElementById("root");
ReactDOM.render(<App />, root);
</script>

onClick 기능을 주고 싶다. function App() {
const [value, setValue] = React.useState("Save Changes");
const changeValue = () => setValue("Revert Changes");
return (
<div>
<Btn text={value} onClick={changeValue} />
<Btn text="Continue" />
</div>
);
}
onClick={changeValue} 이 녀석은 놀랍게도 하나의 Prop이다. <Btn text={value} onClick={changeValue} /> 라인 안에다가 인라인 CSS... 라고 해야하나 아무튼 style={{}}을 작성해도 적용되지 않는다. <script type="text/babel">
function Btn({ text, onClick }) {
console.log("text : " + text);
return (
<button
onClick={onClick}
style={{
backgroundColor: "tomato",
color: "white",
padding: "10px 20px",
margin: "3px",
border: 0,
borderRadius: "10px",
}}
>
{text}
</button>
);
}
function App() {
const [value, setValue] = React.useState("Save Changes");
const changeValue = () => setValue("Revert Changes");
return (
<div>
<Btn text={value} onClick={changeValue} />
<Btn text="Continue" />
</div>
);
}
const root = document.getElementById("root");
ReactDOM.render(<App />, root);
</script>

결론적으로 말해 Prop으로 보낼 수 있는 데이터는 다양하다.
텍스트나 불린 값은 물론, 함수까지 보낼 수 있다는 말이다.
그리고 받은 Prop을 어디에 넣을지 결정하는 것은 개발자 자신이다.
리액트는 거기에 관여하지는 않는다.
function App() {
const [value, setValue] = React.useState("Save Changes");
const changeValue = () => setValue("Revert Changes");
return (
<div>
<Btn text={value} onClick={changeValue} />
<Btn text="Continue" />
</div>
);
}
<Btn text="Continue" /> 까지 리프레시 되는 것이 별로라는 거다. function Btn({ text, onClick }) {
console.log(text, "was rendered.");
return (
<button
onClick={onClick}
style={{
backgroundColor: "tomato",
color: "white",
padding: "10px 20px",
margin: "3px",
border: 0,
borderRadius: "10px",
}}
>
{text}
</button>
);
}

<script type="text/babel">
function Btn({ text, onClick }) {
console.log(text, "was rendered.");
return (
<button
onClick={onClick}
style={{
backgroundColor: "tomato",
color: "white",
padding: "10px 20px",
margin: "3px",
border: 0,
borderRadius: "10px",
}}
>
{text}
</button>
);
}
const MemorizedBtn = React.memo(Btn);
function App() {
const [value, setValue] = React.useState("Save Changes");
const changeValue = () => setValue("Revert Changes");
return (
<div>
<MemorizedBtn text={value} onClick={changeValue} />
<MemorizedBtn text="Continue" />
</div>
);
}
const root = document.getElementById("root");
ReactDOM.render(<App />, root);
</script>

메모 기능이 없을 때,
리액트의 최상위 컴포넌트(부모 컴포넌트)에서 그 어떤 부분에서 작은 State 변경 사항이 발생하기만 한다면, 모든 자녀들은 Re-render된다.
이로 인하여 어플리케이션이 매우 느려질 수 있는 것이다.
몇 천 개의 컴포넌트를 사용하는 큰 어플리케이션을 상상해보자.
<script type="text/babel">
function Btn({ text, fontSize }) {
return (
<button
style={{
backgroundColor: "tomato",
color: "white",
padding: "10px 20px",
margin: 3,
border: 0,
borderRadius: 10,
fontSize: fontSize,
}}
>
{text}
</button>
);
}
function App() {
return (
<div>
<Btn text="Save Changes" fontSize={18} />
<Btn text={18} fontSize={"Continue"} />
</div>
);
}
const root = document.getElementById("root");
ReactDOM.render(<App />, root);
</script>

우선, 파일 안에
<script src="https://unpkg.com/prop-types@15.6/prop-types.js"></script>를 설정!
<script type="text/babel">
function Btn({ text, fontSize }) {
return (
<button
style={{
backgroundColor: "tomato",
color: "white",
padding: "10px 20px",
margin: 3,
border: 0,
borderRadius: 10,
fontSize: fontSize,
}}
>
{text}
</button>
);
}
Btn.propTypes = {
text: PropTypes.string,
fontSize: PropTypes.number,
};
function App() {
return (
<div>
<Btn text="Save Changes" fontSize={18} />
<Btn text={18} fontSize={"Continue"} />
</div>
);
}
const root = document.getElementById("root");
ReactDOM.render(<App />, root);
</script>

Btn.propTypes = {
text: PropTypes.string,
fontSize: PropTypes.number.isRequired,
};



PropType을 통해 오류가 고쳐지지는 않지만 Console로 Warning을 줄 수는 있다.