React 란 ?
: 사용자 인터페이스를 구축하기 위한 효율적이고 유연한 js 라이브러리
컴포넌트를 이용해서 복잡한 UI를 구성하도록 돕는다
render() 함수를 통해 화면에서 보고자하는 내용을 반환
<h1>
Click me!
</h1>
const paragraphs = (
<div id="i-am-the-outermost-element">
<p>I am a paragraph.</p>
<p>I, too, am a paragraph.</p>
</div>
);
var myList = "Hello";
ReactDOM.render(myList, document.getElementById('app'));
< h 1 > className="big">Hey < / h 1 >
< br / > < img / >
ReactDOM.render(< h1 >2+3< /h1 >,
document.getElementById('app')
);
===== 2+3 으로 출력
Everything inside of the curly braces will be treated as regular JavaScript.
ReactDOM.render(< h1 >{ 2+3 }< /h1 >,
document.getElementById('app')
);
===== 5로 출력
var name = "claire";
ReactDOM.render(< h1 >{name}< /h1 >,
document.getElementById('app')
);
===== 태그안에서 변수 name을 불러올때 { }사용. claire로 출력
function myFunc() {
alert('Make myFunc the pFunc... omg that was horrible i am so sorry');
}
< img onClick={ myFunc } / >
== 클릭시 function
const headline = (
< h1 >
{ age >= drinkingAge ? 'Buy Drink' : 'Do Teen Stuff' }
< /h1 >
);
=== 맞으면 < h1 > Buy Drink < /h1 >, 아니면 < h1 > Do Teen Stuff < /h1 >
function coinToss () {
return Math.random() < 0.5 ? 'heads' : 'tails';
}
const pics = {kitty: url, doggy: url2 };
const img = <img src={pics[coinToss() === 'heads' ? 'kitty' : 'doggy']} />;
ReactDOM.render(img, document.getElementById('app'));
==== pics의 array[ ] 호출 ' ' . 위 function coinToss가 heads일 경우 kitty 이미지 출력
const people = ['Rowe', 'Prevost', 'Gare'];
const peopleLis = people.map(
person => < li >{person}< /li >
);
ReactDOM.render(< ul >{peopleLis}< /ul >,document.getElementById('app'));
<출력>
Rowe
Prevost
Gare
const h1 = < h1 >Hello world< /h1 >;
const h1 = React.createElement(
"h1",
null,
"Hello, world"
);
import React from 'react';
import ReactDOM from 'react-dom';
class + class이름(UpperCamelCase로 작성) + extends React.Component {}
method = render() {}
class MyComponentClass extends React.Component {
render() {
return < h1 >Hello world< /h1 >;
}
}
ReactDOM.render(
< MyComponentClass / >,
document.getElementById('app')
)