JSX
in Notes / Front-end / React
JSX의 기본 규칙
2022-08-24
- JSX : HTML같이 보이는 태그를 JS코드로 변환해줌 (tryout in Babel)
- RULES TO FOLLOW
- ALL tags must be closed (either self-cloisng or closing tag)
- only return 1 TAG (div 또는 <></> (fragment)로 감싸주기)
- using JS variables or syntax within JSX : put it inside curly braces {}
- class X, className O
- inline CSS -> make const sytle in JS part, put it in inline CSS
- comment :
{ /* commented content */ }
EXAMPLE
// file: "App.js" import React from 'react'; function App(){ const name = 'hypnotes' const style = { background: 'purple', } return ( {/* JSX STARTS */} <div style = {style}> <div className = 'my-style'> {name} </div> </div> ) }
- RULES TO FOLLOW
- PROPS : send certain values to other components
App.js
// file: "App.js" import Hello from './Hello'; function App(){ return ( <Hello name = "hypnotes" color="purple"> ) }
props.PROPSNAME
Example
// file: "Hello.js" import Hello from './Hello'; function Hello(props){ //use props.PROPSNAME return ( return <div style = { props.color }>HELLO, { props.name }</div> ) } export default Hello;
{ propsNAME}
비구조 할당Example
// file: "Hello.js" import Hello from './Hello'; function Hello( {name, color} ){ //use props.PROPSNAME return ( return <div style = { color }>HELLO, { name }</div> ) } export default Hello;
- Default Props
Hello.defaultProps = { name : '이름없음' };
- JSX :