Skip to content

Commit 065ea86

Browse files
Functional Components
1 parent 1979d62 commit 065ea86

File tree

1 file changed

+112
-1
lines changed

1 file changed

+112
-1
lines changed

react.js

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,4 +941,115 @@ export class Clock extends React.Component {
941941
clearInterval(this.intervalID);
942942
}
943943
}
944-
944+
945+
946+
947+
// FUNCTIONAL COMPONENTS
948+
// state, ref and lyfecycle arrived in react v16.8 with hooks
949+
// still can't use componentDidError and getSnapshotBeforeUpdate
950+
951+
// React Components
952+
const FunctionalComponent() {
953+
return <h1>Hello world</h1>;
954+
};
955+
const FunctionalComponent = () => (
956+
<h1>Hello world</h1>;
957+
);
958+
959+
960+
961+
// React Component multiline
962+
const FunctionalComponent() {
963+
return (
964+
<div>
965+
<h1>Hello world</h1>
966+
<p>How you doing?</p>
967+
</div>
968+
);
969+
}
970+
const FunctionalComponent = () => (
971+
<div>
972+
<h1>Hello world</h1>
973+
<p>How you doing?</p>
974+
</div>
975+
);
976+
977+
978+
// Render Component
979+
import React from "react";
980+
const FunctionalComponent = () => { return <h1>Hello world</h1>; };
981+
ReactDOM.render(<FunctionalComponent />, document.getElementById('app'));
982+
// OR
983+
import React from "react";
984+
const FunctionalComponent = () => { return <h1>Hello world</h1>; };
985+
export default FunctionalComponent;
986+
// OR
987+
import React from "react";
988+
export default function FunctionalComponent() { return <h1>Hello world</h1>; };
989+
990+
991+
// Pay attention to the export method !!!
992+
export default function ComponentName() {};
993+
import ComponentName from './path';
994+
995+
const ComponentName = () => {};
996+
export default ComponentName;
997+
import ComponentName from './path';
998+
999+
export function ComponentName() {};
1000+
import { ComponentName } from './path';
1001+
1002+
export const ComponentName = () => {};
1003+
import { ComponentName } from './path';
1004+
1005+
1006+
// PROPS
1007+
import PropTypes from 'prop-types'; // needs 'npm install prop-types'
1008+
const FunctionalComponent = (props) => {
1009+
return <h1>Hello, {props.name}</h1>;
1010+
};
1011+
// OR
1012+
const FunctionalComponent = ({ name }) => {
1013+
return <h1>Hello, {name}</h1>;
1014+
};
1015+
// OR
1016+
const FunctionalComponent = ({ name, ...props }) => {
1017+
return <h1>Hello, {name} {props.surname}</h1>;
1018+
};
1019+
// prop types arrayOf(number), objectOf(string)) -> https://reactjs.org/docs/typechecking-with-proptypes.html
1020+
FunctionalComponent.propTypes = {
1021+
name: PropTypes.string.isRequired,
1022+
age: PropTypes.number,
1023+
fun: PropTypes.bool,
1024+
arr: PropTypes.array,
1025+
obj: PropTypes.obj,
1026+
el: PropTypes.element,
1027+
one: PropTypes.oneOf([number, string])
1028+
};
1029+
// default props
1030+
FunctionalComponent.defaultProps = { age: 16 };
1031+
<FunctionalComponent name="John" age={25} fun={true} arr={[1, 2]} obj={{yes: 'no'}} el={<AnotherComponent />} one={1} />
1032+
1033+
1034+
// PROPS FROM PARENT TO CHILD
1035+
import { ChildComponent } from './ChildComponent';
1036+
const ParentComponent = () => {
1037+
return <ChildComponent name="John" />;
1038+
};
1039+
export const ChildComponent = ({ name, ...props }) => {
1040+
return <h1>Hello, {name}</h1>
1041+
};
1042+
1043+
1044+
// PROPS FROM CHILD TO PARENT
1045+
import { ChildComponent } from './ChildComponent';
1046+
const ParentComponent = () => {
1047+
const getFromChild = (data) => {
1048+
console.log(data);
1049+
}
1050+
return <ChildComponent func={getFromChild} />;
1051+
};
1052+
export const ChildComponent = ({ func, ...props }) => {
1053+
func('This is data')
1054+
return <></>
1055+
};

0 commit comments

Comments
 (0)