PlacePositionBox.tsx
Go to src/components/PlacePositionBox.tsx
where we will be building our next component to handle inputs.
Importing Dependencies:
At the top of the file, import the following dependencies:
Setting up our component
Define the component
PlacePositionBox
using the functional component syntax and destructuring theprops
object forpubkey
:
Use the
useState
hook to initialize the state forinputValue
andamount
. Set the initial value forinputValue
to'Enter Amount...'
and foramount
to'0'
:
Use the
useEffect
hook to specify the component should re-render whenever thepubkey
changes:
If the
pubkey
is equal to'Loading'
, return a loading message:
Define a
handleChange
function that sets theinputValue
andamount
to the value of theevent.target.value
:
Now, handle the change of the input value by creating the
handleChange
function. ThehandleChange
function updates the input value with the entered amount using thesetInputValue
hook and sets the same value toamount
with thesetAmount
hook. If the input value is empty, it sets theinputValue
back to the placeholder text "Enter Amount...".
Returning our component:
Finally, return the component, which is a div
that contains the input field and the two buttons for LONG and SHORT positions. The input field takes the value from inputValue
and the onChange
event is set to handleChange
function. The styles for the input field are set using inline styling.
Now, let's test it!
Go to
src/components/PariBox.tsx
Import the component into the file
Place it at the bottom of our second div:
The output should look similar to this:
Last updated