props are usually defined in parent components. 

class App extends React.Component {
	render() {
		return (
			<div>
				<Hello to="Ringo" from="Paul" />
				<Hello to="Cher" from="Sonny" />
			</div>
		);
	}
}


ReactDOM.render(<App />, document.getElementById('root'));
class Hello extends React.Component {
    render() {
        //console.log(this.props.to);// properties that passed

        // don't change the prop here.
        // ex) this.props.to = "Blue"; -> this will give you red lines, lots of err!!!

        const props = this.props;
        return <h1>Hello {props.to} from {props.from}!</h1>;
    }
}

 

'react' 카테고리의 다른 글

Props : slot machine exercise  (0) 2020.06.13
Props : types of props  (0) 2020.06.13
Introducing JSX : basic concept of React layout  (0) 2020.06.13
Introducing JSX : conditionals  (0) 2020.06.13
Introducing JSX : basic  (0) 2020.06.13

1. Html code that we push react components

: every react component push inside of <div class="root">

: JS code orders are impotent!  

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>First Component</title>
</head>

<body>
  <div id="root"></div>
  <script src="https://unpkg.com/react/umd/react.development.js"></script>
  <script src="https://unpkg.com/react-dom/umd/react-dom.development.js"></script>

  <script src="https://unpkg.com/babel-standalone"></script>

  <!--JS orders are Matter!!!!-->
  <script src="Hello.js" type="text/jsx"></script>
  <script src="NumPicker.js" type="text/jsx"></script>
  <script src="index.js" type="text/jsx"></script>

  </script>
</body>

</html>

 

 

2. index.js file that render other components.

: one component in one file!!!!!! wrap with <div> you have several components, React treats  everything in <div> as one component.

: we make App component and render the others in that file.

: this is usually the only thing rendered in index.js

 

class App extends React.Component {
	render() {
		return (
			<div>
				<Hello />
				<NumPicker />
			</div>
		);
	}
}


ReactDOM.render(<App />, document.getElementById('root'));

 

 

3. Actual React components

class Hello extends React.Component {
    render() {
        return (
            <div>
                <h1>Hello there!</h1>
            </div>
        );
    }
}
function getNum() {
    return Math.floor(Math.random() * 10) + 1;
}

class NumPicker extends React.Component {
    render() {
        const num = getNum();
        let msg;
        if (num === 7) {
            msg =
                <div>
                    <p>Lucky!</p>
                    <img src="https://thumbnail.10x10.co.kr/webimage/image/basic600/176/B001768023-4.jpg?cmd=thumb&w=500&h=500&fit=true&ws=false" />
                </div>
        } else {
            msg = <p>Try again!</p>
        }

        return (
            <div>
                <h1>Your number is...{num}</h1>
                {msg}
            </div>
        );
    }
}

'react' 카테고리의 다른 글

Props : types of props  (0) 2020.06.13
Introducing Props! and more..  (0) 2020.06.13
Introducing JSX : conditionals  (0) 2020.06.13
Introducing JSX : basic  (0) 2020.06.13
class component vs function component  (0) 2020.06.13

You can add conditinal statement in JSX like this.

 

function getNum() {
    return Math.floor(Math.random() * 10) + 1;
}


// option 1-a,b
class NumPicker extends React.Component {
    render() {
        const num = getNum();
        return (
            <div>
                <h1>Your number is...{num}</h1>
                <p>
                    {num === 7 ? 'Lucky!' : 'Try again'}</p>
                {
                    num === 7
                        ? <img src="https://thumbnail.10x10.co.kr/webimage/image/basic600/176/B001768023-4.jpg?cmd=thumb&w=500&h=500&fit=true&ws=false" />
                        : null
                    // 이거도 가능
                    // num === 7 && <img src="https://thumbnail.10x10.co.kr/webimage/image/basic600/176/B001768023-4.jpg?cmd=thumb&w=500&h=500&fit=true&ws=false" />
                }
            </div>
        );
    }
}

// option 2
class NumPicker extends React.Component {
    render() {
        const num = getNum();
        let msg;
        if (num === 7) {
            msg =
                <div>
                    <p>Lucky!</p>
                    <img src="https://thumbnail.10x10.co.kr/webimage/image/basic600/176/B001768023-4.jpg?cmd=thumb&w=500&h=500&fit=true&ws=false" />
                </div>
        } else {
            msg = <p>Try again!</p>
        }

        return (
            <div>
                <h1>Your number is...{num}</h1>
                {msg}
            </div>
        );
    }
}




ReactDOM.render(<NumPicker />, document.getElementById('root'));

'react' 카테고리의 다른 글

Props : types of props  (0) 2020.06.13
Introducing Props! and more..  (0) 2020.06.13
Introducing JSX : basic concept of React layout  (0) 2020.06.13
Introducing JSX : basic  (0) 2020.06.13
class component vs function component  (0) 2020.06.13

JSX is much more strict that html, must be closed with </>

  • <b>...</b>
  • <img src='..' /> : self closing tag is must

if want to render several components, you need to wrap it up in div, section or form.

 

 

function getMood() {
	const moods = ['happy', 'angry', 'sad', 'silly'];
	return moods[Math.floor(Math.random() * moods.length)];
}


class JSXDemo extends React.Component {
	render() {
		return (
			<div>
				<h1>My Page</h1>
				<img src="https://images.unsplash.com/photo-1505909182942-e2f09aee3e89?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1052&q=80" />
				<p> My numbe is: {2 * 8} </p>
				<p>My current mood is: {getMood()}</p>
			</div>
		);// we can use js code inside of {}
	}
}


ReactDOM.render(<JSXDemo />, document.getElementById('root'));

'react' 카테고리의 다른 글

Props : types of props  (0) 2020.06.13
Introducing Props! and more..  (0) 2020.06.13
Introducing JSX : basic concept of React layout  (0) 2020.06.13
Introducing JSX : conditionals  (0) 2020.06.13
class component vs function component  (0) 2020.06.13

class Hello extends React.Component {
	render() {
		return (//we only can return one obj-> so we must wrap components with div or else
			<div>
				<h1>Hello there!</h1>
				<h1>Hello there!</h1>
				<h1>Hello there!</h1>
			</div>
		);
	}
}

 

 

Historically, function component couldn't use important features (-State, Lifecycle Method)

BUT, with Hook(introduced on 2019)!! we can write full-featured function.

 

 

function Hello() {
	return (
		<div>
			<h1>Hello there!</h1>
			<h1>Hello there!</h1>
			<h1>Hello there!</h1>
		</div>
	);
}


ReactDOM.render(<Hello />, document.getElementById('root'));

'react' 카테고리의 다른 글

Props : types of props  (0) 2020.06.13
Introducing Props! and more..  (0) 2020.06.13
Introducing JSX : basic concept of React layout  (0) 2020.06.13
Introducing JSX : conditionals  (0) 2020.06.13
Introducing JSX : basic  (0) 2020.06.13