44
loading...
This website collects cookies to deliver better user experience
npx create-react-app --template typescript typescript-with-react
cd typescript-with-react/
code .
npm start
.ts
or .tsx
extension. That denotes that those files are transpiled in typescript.IState
(You can name it whatever you like). IState
is where we will write our type definition of how we want the state variables to be, which in this case is an array of objects. To denote that we add square bracket after the type definitions. And then while using useState, add <IState["form"]>
which denotes that the state should be accepting values in the specified format only(IState format in this case which is taking the object 'form' as input format)
const [counter, setCounter] = useState<{name:string,rate:number,review?:string}[]>([])
review?:string
where the question mark denotes the value of review could either be a string or undefined. However for name
and rate
we have strict type definitions which won't accept anything apart from the assigned type definitions.inputValue:number | string | null
inputValue
can either be a data type of number, string or even a null value <List form={form} />
<Form form={form} setForm={setForm} />
App.tsx
we are sending one object ie. form
to List.tsx
List
component's recieving side now.
import { IState as IProps } from "../App"
const List: React.FC<IProps> = ({ form }) => {
...
}
React.FC<IProps>
after the List
component declaration.IState
under the alias IProps
since we know that the type definitions of the object form
are exactly the same as the IState
object. form
in the parameters and use it in the function component.IState
under the alias Props
, however we have made some customized changes here. Here we created a new interface called IProps
that specifies the type defintion of props incoming since we had to specify the type of setForm
.
We mention form: Props["form"]
which means form should be assigned the type definition of IState
which is imported under the alias Props
And then similarly we will now do it for setForm
Protip : to know the type definitions of something you don't have a clue about, just hover over that element like this and copy the type definitions.
Props["form"]
, we can cut short the type definition of setForm
and write it this way instead
setForm: React.Dispatch<React.SetStateAction<Props["form"]>>
form
and setForm
in the parameters of the Form
function and use it in the component.mapList()
function to map through the array of list and give table row as an output, which is a JSX element.: JSX.Element[]
after the parameters, which denotes that the function is supposed to return an array of JSX elements.JSX.Element[]
.Protip: If you aren't sure what the return type is,hover over the function to know it's return type.
:void
after parameters in this way :const randomFunction = (): void => {
...
}
Form
component:<input className="inputBox" type='text' name="name" value={input.name} onChange={(e) => handleChange(e)} />
<textarea className="inputBox" name="review" value={input.review} onChange={(e) => handleChange(e)}></textarea>
input
tag has a DOM property called onChange
which calls handleChange
when an event is triggered.const handleChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>): void => {
setInput({
...input,
[e.target.name]: e.target.value
})
}
e
will either be of type React.ChangeEvent<HTMLInputElement>
which is what the input
tag will send. e
could also be React.ChangeEvent<HTMLTextAreaElement>
.e
can be written as e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
.:void
to specify that this function won't be returning anything.onClick
event called by the form submit button.<button className="button" type="submit" onClick={(e) => handleClick(e)}>Submit</button>
const handleClick = (e: React.MouseEvent<HTMLButtonElement>): void => {
e.preventDefault();
if (!input.name || !input.rate) {
return
}
setForm([...form, {
name: input.name,
rate: parseInt(input.rate),
review: input.review
}])
}
handleChange
function the handleClick
function takes a proper type definition of e
which in this case is React.MouseEvent<HTMLButtonElement>
.