javascript - React.js How do I get the id in props with the getElementById method? -
start learn react.js , encounter following problem: enter image description here
i want div
element using getelementbyid
doesn't work , console alerts can not read property 'props' of null
. how can div
element? has troubled me several hours. thanks.
the whole idea react don't access dom, work virtual dom.
to achieve trying accomplish (which little unclear since code incomplete), you'll want use stateful component stores user input on this.state
. check out this page on react forms.
you'll want this, tracking user input storing on component:
class nameform extends react.component { constructor(props) { super(props); this.state = {value: ''}; this.handlechange = this.handlechange.bind(this); this.handlesubmit = this.handlesubmit.bind(this); } handlechange(event) { this.setstate({value: event.target.value}); } handlesubmit(event) { alert('a name submitted: ' + this.state.value); event.preventdefault(); } render() { return ( <form onsubmit={this.handlesubmit}> <label> name: <input type="text" value={this.state.value} onchange={this.handlechange} /> </label> <input type="submit" value="submit" /> </form> ); } }
Comments
Post a Comment