There’s a main App component with two child components SearchBox and SearchResult. App keeps the search term in its state (since it will pass it down the SearchResult), so I have to make a handler to update it when the form in the SearchBox is submitted.
Now, since the event handler is attached to the <form>, it’s what the event object points to. I figured that a way to derive the search term in the textbox is by using event.target[0].value.
This works, but are there other ways of getting the value from the textbox? I tried event.target.getElementById('search'), but the console says that it’s not a function. I think you can also keep a separate state for SearchBox for the search term, but that means writing more code to update it as the value in the textbox changes (and also, there are gonna be two states for basically the same value, which doesn’t feel right).
I was also thinking about keeping the value of the textbox in the SearchBox component’s state, but it’s already kept by the parent App component’s state (because another component in App needs that value). Managing two states for the same value doesn’t feel right.
Yeah, that is indeed the more ‘reactive’ way to do it, but when I tried to use that on a separate component in the stock charting project I could not make it work, briefly, my main component (Home) calls a dumb component (Addstock) that just displays a form and sends the value of the form back to Home on enter or submit, simple enough right ? not really, at least it wasn’t for me, here are the props and how I called Addstock from Home:
And Addstock has this (amongst other things) as props on FormControl
<FormControl ref={this.props.inputRef}
Now here is the kicker, back in the home component if I use
findDOMNode(this.inputNode).value
I get the value of the input no problem, but , if I try using the ‘reactive’ way
this.inputNode.value
I get undefined for the input value, I have tried many different variations and bindings and could not get it to work without findDOMNode unfortunately