Copy npx create-react-app my-app --template typescript
# or
yarn create react-app my-app --template typescript
Copy import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
ReactDOM.render(<React.StrictMode><App /></React.StrictMode>,document.getElementById('root'));
serviceWorker.unregister();
Copy import React from 'react';
import ReactDOM from 'react-dom';
import './App.css';
import {ExtendedSelectableButton, SelectableButtonTypes} from './components/ExtendedSelectableButton/ExtendedSelectableButton'
const App = () => {
return (
<div className="App">
<header className="App-header">
<ExtendedSelectableButton type={SelectableButtonTypes.Important} text="Select me!!" action={ (selected) => {
console.log(selected)
}} />
</header>
</div>
);
}
export default App;
C:\Users\Administrator\Desktop\hello-tsx\src\components\ExtendedSelectableButton\ExtendedSelectableButton.tsx
Copy import React, { useState } from 'react';
import ReactDOM from 'react-dom';
/** A set of groupped constants */
enum SelectableButtonTypes {
Important = "important",
Optional = "optional",
Irrelevant = "irrelevant"
}
interface IButtonProps {
text: string,
/** The type of button, pulled from the Enum SelectableButtonTypes */
type: SelectableButtonTypes,
action: (selected: boolean) => void
}
const ExtendedSelectableButton = ({text, type, action}: IButtonProps) => {
let [selected, setSelected] = useState(false);
return (
<button className={"extendedSelectableButton " + type + (selected? " selected" : "")}
onClick={
_ => {
setSelected(!selected)
action(selected)
}
}>
{text}
</button>
)
}
/** Exporting the component AND the Enum */
export { ExtendedSelectableButton, SelectableButtonTypes}