My React/Typescript app has several sections. So the
export default function App() {}
has become very long. All tutorials I have watched do not go further to refactoring this. And I have had trouble searching the right thing.
But I am having trouble refactoring the first element.(just a snippet)
my code currently: App.tsx
import React from 'react';
import Button from 'react-bootstrap/Button';
export default function App() {
const [count, setCount] = React.useState(0);
const minusnum = () => {
if (count > 0) setCount(count - 1);
};
const addnum = () => {
setCount(count + 1);
};
return (
<div>
<Button onClick={minusnum}>minus</Button>
<Button onClick={addnum}>add</Button>
<input type="number" min="0" step="1" name="clicks" value={count} onChange={(event) => {const value = Number(event.target.value); setCount(value);}}></input>
</div> );
}
Now I want App.tsx to look something like:
import React from 'react';
import Button from 'react-bootstrap/Button';
import Counter from "./parts/Counter";
export default function App() {
<CounterSection>
<Counter/>
</CounterSection>
}
and inside ./parts/Counter there is an index.tsx file
import React from 'react';
import Button from 'react-bootstrap/Button';
const [count, setCount] = React.useState(0);
const minusnum = () => {
if (count > 0) setCount(count - 1);
};
const addnum = () => {
setCount(count + 1);
};
const Index = () => {
return (
<div>
<Button onClick={minusnum}>minus</Button>
<Button onClick={addnum}>add</Button>
<input type="number" min="0" step="5" name="clicks" value={count} onChange={(event) => {const value = Number(event.target.value); setCount(value);}}></input>
</div> )
};
export default Index;
Which ends up showing nothing. how do I deal with the event handling stuff in the index file?