Note that the tutorial is written in JavaScript, not TypeScript.
However, I found the solution for this correctly (the OP answer is very cumbersome). Basically, you should make two changes from the tutorial code. For reference, here is the code from the tutorial as I write this:
var author = React.findDOMNode(this.refs.author).value.trim();
First change:
this.refs.author
becomes
this.refs["author"]
I am new to TypeScript, but I assume that he prefers to use indexer syntax over property syntax for objects that are designed to prevent their true properties from being declared forward.
Secondly, and most importantly,
React.findDOMNode
becomes
React.findDOMNode<HTMLInputElement>
Basically here we have to specifically tell TypeScript which element we are requesting. Use the full code to search for a complete list of available items. I assume that it covers all the internal components.
Here is the final, working line of code:
var author = React.findDOMNode<HTMLInputElement>(this.refs["author"]).value.trim();
For convenience, the above method is up to the moment when this method first appears in the tutorial (slightly reorganized to avoid calling findDOMNode twice):
handleSubmit(e: React.FormEvent) { e.preventDefault(); var authorInput = React.findDOMNode<HTMLInputElement>(this.refs["author"]); var textInput = React.findDOMNode<HTMLInputElement>(this.refs["text"]); var author = authorInput.value.trim(); var text = textInput.value.trim(); if (!text || !author) return; authorInput.value = textInput.value = ""; }
nlaq
source share