2016-08-18 27 views
0

我是比較新的反應,儘管見狀例如在視頻教程的工作,我不能得到它的工作:無法通過裁判獲得輸入形式的價值做出反應

import React from 'react'; 
import Form from 'muicss/lib/react/form'; 
import Input from 'muicss/lib/react/input'; 
import Textarea from 'muicss/lib/react/textarea'; 
import Button from 'muicss/lib/react/button'; 

export default class blogCreate extends React.Component { 
addBlog(event) { 
    event.preventDefault(); 
     var blogTitle = this.refs.title.value; 
     var blogContent = this.refs.content.value; 
     var user = Meteor.userId(); 
     console.log(blogTitle, blogContent, user); 
     Meteor.call('blogAdd', blogTitle, blogContent, user); 
} 
render() { 
    return(
     <Form onSubmit={this.addBlog.bind(this)}> 
      <legend>Add a blog post</legend> 
      <Input name='title' label="Title" ref="title" /> 
      <Textarea name='text' label="Text" ref="content" /> 
      <Button variant="raised" type="submit">Submit</Button> 
     </Form> 

    ) 
    } 
} 

當我輸出console.log(blogTitle,blogContent,user)用戶標識正確顯示,但blogTitle和blogContent未定義。如果我console.log(this.ref.title)並查看該對象,則該對象上沒有value屬性,儘管標題的內容已被填充。

我使用的是反應15.3.0。

回答

0

裁判=「標題」和ref =「內容」正在與和組件,而不是他們造成的div相關聯的,所以這就是爲什麼我的參考值是空的。當我用實際的組件替換這些組件時,它按預期工作。

0

refs根據您是否在DOM組件或複合組件上使用它們而返回不同的值。

你的情況看起來雖然<Input /><Textarea />是如此this.refs.titlethis.refs.content將引用後盾的情況下,而不是DOM節點複合材料部件。

您可以通過使用ReactDOM.findDOMNode(例如, ReactDOM.findDOMNode(this.refs.title).value(假設<Input />組件返回<input />節點作爲根元素,否則您可能需要查詢輸入,例如ReactDOM.findDOMNode(this.refs.title).getElementByTagName('input').value