Skip to content

Commit 3fa700d

Browse files
Props
1 parent 6811de0 commit 3fa700d

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

react.js

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,4 +676,153 @@ class OMG extends React.Component {
676676
}
677677
}
678678
ReactDOM.render(<ProfilePage />, document.getElementById('app'));
679+
680+
681+
682+
// THIS.PROPS
683+
684+
// access component properties
685+
class PropsDisplayer extends React.Component {
686+
render() {
687+
<p>{JSON.stringify(this.props)}</p>
688+
}
689+
}
690+
ReactDOM.render(<PropsDisplayer name="Rem" age={26} alive={true} />, document.getElementById('app'));
691+
692+
// show a specific property
693+
class Greeting extends React.Component {
694+
render() {
695+
return <h1>Hi there, {this.props.firstName}!</h1>;
696+
}
697+
}
698+
ReactDOM.render(<Greeting firstName='Rem' />, document.getElementById('app'));
699+
700+
701+
// imported component with props
702+
import React from 'react';
703+
export class Greeting extends React.Component {
704+
render() {
705+
return <h1>Hi there, {this.props.name}!</h1>;
706+
}
707+
}
708+
import React from 'react';
709+
import ReactDOM from 'react-dom';
710+
import { Greeting } from './Greeting';
711+
class App extends React.Component {
712+
render() {
713+
return (
714+
<div>
715+
<h1>Hello and Welcome to The Newzz!</h1>
716+
<Greeting name="Rem" />
717+
</div>
718+
);
719+
}
720+
}
721+
ReactDOM.render(<App />, document.getElementById('app'));
722+
723+
724+
// conditionals with props
725+
import React from 'react';
726+
export class Welcome extends React.Component {
727+
render() {
728+
if (this.props.name == 'Wolfgang Amadeus Mozart') {
729+
return (
730+
<h2>hello sir it is truly great to meet you here on the web</h2>
731+
);
732+
} else {
733+
return (
734+
<h2>WELCOME "2" MY WEB SITE BABYYY!!!!!</h2>
735+
);
736+
}
737+
}
738+
}
739+
import React from 'react';
740+
import ReactDOM from 'react-dom';
741+
import { Welcome } from './Welcome';
742+
class Home extends React.Component {
743+
render() {
744+
return <Welcome name='Ludwig van Beethoven' />;
745+
}
746+
}
747+
ReactDOM.render(<Home />, document.getElementById('app'));
748+
749+
750+
// imported event handler
751+
import React from 'react';
752+
export class Button extends React.Component {
753+
render() {
754+
return (
755+
<button onClick={this.props.onClick}>Click me!</button>
756+
);
757+
}
758+
}
759+
import React from 'react';
760+
import ReactDOM from 'react-dom';
761+
import { Button } from './Button';
762+
class Talker extends React.Component {
763+
handleClick() {
764+
let speech = '';
765+
for (let i = 0; i < 10000; i++) {
766+
speech += 'blah ';
767+
}
768+
alert(speech);
769+
}
770+
render() {
771+
return <Button onClick={this.handleClick} />;
772+
}
773+
}
774+
ReactDOM.render(<Talker />, document.getElementById('app'));
775+
776+
777+
// props children
778+
import React from 'react';
779+
export class List extends React.Component {
780+
render() {
781+
let titleText = `Favorite ${this.props.type}`;
782+
if (this.props.children instanceof Array) {
783+
titleText += 's';
784+
}
785+
return (
786+
<div>
787+
<h1>{titleText}</h1>
788+
<ul>{this.props.children}</ul>
789+
</div>
790+
);
791+
}
792+
}
793+
import React from 'react';
794+
import ReactDOM from 'react-dom';
795+
import { List } from './List';
796+
class App extends React.Component {
797+
render() {
798+
return (
799+
<div>
800+
<List type='Living Musician'>
801+
<li>Sachiko M</li>
802+
<li>Harvey Sid Fisher</li>
803+
</List>
804+
<List type='Living Cat Musician'>
805+
<li>Nora the Piano Cat</li>
806+
</List>
807+
</div>
808+
);
809+
}
810+
}
811+
ReactDOM.render(<App />, document.getElementById('app'));
812+
813+
814+
// default props; if props doesn't exist, it will take the default value
815+
import React from 'react';
816+
import ReactDOM from 'react-dom';
817+
class Button extends React.Component {
818+
render() {
819+
return (
820+
<button>
821+
{this.props.text}
822+
</button>
823+
);
824+
}
825+
}
826+
Button.defaultProps = {text: 'I am a button'};
827+
ReactDOM.render(<Button text="heya" />, document.getElementById('app'));
679828

0 commit comments

Comments
 (0)