generated from ginierlo/TeamProject
-
Notifications
You must be signed in to change notification settings - Fork 3
Base: React with Typescript
Melvyn M edited this page Feb 3, 2021
·
2 revisions
Exemple of basic component with type.
// App.tsx
// exemple of Exemple using
function App() {
return (
<div>
<Exemple
size="small"
onClick={text => console.log(text)}
displaySecondButton={true}>
<p>Je suis le `children`</p>
</Exemple>
</div>
);
}
// src/components/Exemple.tsx
type ExempleProps = {
// size can now only be a string "small" or "big"
size: 'small' | 'big';
children: React.ReactElement; // can be a string, a component...
onClick: (text: string) => void;
displaySecondButton: boolean;
// the `?` is for say the lists in not required
lists?: Array<string>;
};
export default function Exemple({
size,
children,
onClick,
displaySecondButton,
lists,
}: ExempleProps) {
// a component state
const [input, setInput] = React.useState<string>('');
return (
<div>
<input value={input} onChange={event => setInput(event.target.value)} />
<button
// typescript know now size can be only small or big
style={{ height: size === 'small' ? 14 : 20 }}
// input is a string, so it's works
onClick={() => onClick(input)}>
Click me
</button>
{/* conditional rendering */}
{displaySecondButton && <button>Second button</button>}
{/* show the children */}
<div>{children}</div>
{/* list rendering */}
<ul>
{/* list is not required, so I check if exist, else I display a message */}
{lists ? (
lists.map(e => <li>Element: {e}</li>)
) : (
<p>no list provided</p>
)}
</ul>
</div>
);
}
- Home
- Rapport de travail Groupe "THYREL" sprint 1
- Rapport de travail Groupe "THYREL" sprint 2
- Rapport de travail Groupe "THYREL" sprint 3
- Rapport de travail Groupe "THYREL" sprint 4
- Rapport de travail Groupe "THYREL" sprint 5
- Rapport de travail Groupe "THYREL" sprint 6
- Rapport de travail Groupe "THYREL" sprint 7
- Rapport de travail Groupe "THYREL" sprint 8 - FINAL