2017-07-27 66 views
0

我想出來的變量中的組件但是我收到以下錯誤,在輸出各種反應成分

無效的正則表達式:缺少/

我的代碼如下,

class Profile extends React.Component { 
    constructor() { 
    super(); 
    this.state = { 
     user:user 
    } 
    this.updateAvatar = this.updateAvatar.bind(this); 
    this.formatName = this.formatName.bind(this); 
    } 

    render() { 
    let user_name = this.formatName(this.state.user.first_name, this.state.user.last_name); 
    console.log(user_name); 
    return (
     <Avatar image={this.state.user.avatar} 
       changeImageOnClick={this.updateAvatar} /> 
     <p>{ user_name }</p> 
    ) 
    } 

    formatName(first_name, last_name) { 
    console.log(first_name); 
    return first_name + " " + last_name; 
    } 

    updateAvatar() { 
     this.setState({user:{avater:'new'}}) 
    } 
} 

無法理解我爲什麼會得到這個錯誤,這應該是如此簡單做:(

+1

這是一個奇怪的錯誤來獲得,但無論哪種方式在渲染應該被包裹在一個單一的元素的兄弟元素(即把'div'他們周圍)。這不是我期望從中看到的錯誤,但它仍然需要修復。 – ivarni

回答

1

您不能返回同一級別上的兩個節點。只有一個,所以你需要包裝它。 試試這個:

class Profile extends React.Component { 
    constructor() { 
    super(); 
    this.state = { 
     user:user 
    } 
    this.updateAvatar = this.updateAvatar.bind(this); 
    this.formatName = this.formatName.bind(this); 
    } 

    render() { 
    let user_name = this.formatName(this.state.user.first_name, this.state.user.last_name); 
    console.log(user_name); 
    return (
     <div> 
     <Avatar image={this.state.user.avatar} 
       changeImageOnClick={this.updateAvatar} /> 
     <p>{ user_name }</p> 
     </div> 
    ) 
    } 

    formatName(first_name, last_name) { 
    console.log(first_name); 
    return first_name + " " + last_name; 
    } 

    updateAvatar() { 
     this.setState({user:{avater:'new'}}) 
    } 
}