gukbi

React

๐ŸŽฏ

Export

Export Example

useState

useState Example

CSS

Inline Style

import React from 'react';
import './App.css';

// ์Šคํƒ€์ผ ๊ฐ์ฒด ์ •์˜
const divStyle = {
  color: 'blue',
  backgroundColor: 'lightgrey'
};

// App ์ปดํฌ๋„ŒํŠธ ์ •์˜
function App() {
  return (
    <div className="App">
      <div style={divStyle}>Styled element</div>
    </div>
  );
}

Global Style

/* styles.css */
.container {
  background-color: lightblue;
  padding: 20px;
}
// App.js
import React from 'react';
import './styles.css';
function App() {
  return <div className="container">Hello, World!</div>;
}

CSS Module

/* App.module.css */
.container {
  background-color: lightblue;
  padding: 20px;
}
// App.js
import React from 'react';
import styles from './App.module.css';

function App() {
  return <div className={styles.container}>Hello, World!</div>;
}

Child Component

Example 1

When want to use custom onclick event to ChildComponent

// You can write code like this
function ChildComponent(props) {
  const handleButtonClick = typeof props?.onClick == 'function' ? props?.onClick : () => {
    alert("Button clicked!");
   };
   // ์‚ผํ•ญ ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ(Using state?A:B) 

  return (
    <div>
	ChildComponent<br/>
      <button onClick={handleButtonClick}>ํด๋ฆญํ•˜์„ธ์š”</button>
    </div>
  );
}

function App(){
	return (
    <div>
     <ChildComponent />
     <ChildComponent onClick={() => {
         alert("Custom onClick!");
         }} />
    </div>
    );
}