2016-02-27 69 views
0

我正在實現一個具有添加表情符號功能的聊天系統。爲此我創建了一個函數,返回一個組件以及文本和圖像。如何調用反應材料中的方法-UI組件

功能IVE使用

test: function(item1) { 
    var texts = item1.message.split(/:\)/g); 
    console.log(texts); 
    var content = []; 
    for(var i = 0; i < texts.length - 1; i++) { 
     content.push(texts[i]); 
     content.push(<img src={Emojis[0].uri}/>); 
    } 
    return content.map(function(emoji) { 
     return (<span> {emoji} </span>) 
    }) 
    }, 

此聊天我使用socketio和節點明確。並且返回的消息顯示在反應組件上。 「線程」賈森包含所有收到的消息(線程內的密鑰是消息,user1)。現在我想要的是在secondaryText中調用上述測試函數,並將{item.message}作爲參數傳遞。但是當它運行時,它會顯示「Uncaught ReferenceError:test is not defined」。

<div> 
     { 

     this.state.threads.map(function(item) { 

      return (<ListItem 
       leftAvatar={<Avatar src="profile pic" />} 
       primaryText={item.user1} 
       secondaryText={test({item})} 
       secondaryTextLines={2} 
      /> 
      ); 

     }) 
     } 
     </div> 

整個反應成分

const Threads = React.createClass({ 

    getInitialState: function() { 
    return { 
     threads: ThreadStore.getmessages() 
    } 
    }, 

    userlistio:function(){ 
     console.log(" awooooo"); 
    socket.on('chatList',function(data){ 
     console.log(data.Userlist+" awa!"); 
    }.bind(this)); 
}, 

    componentDidMount: function() { 
     ThreadStore.addChangeListener(this._onChange); 

    }, 
    _onChange: function() { 
     this.setState({ 
      threads: ThreadStore.getmessages() 
     }); 

    }, 
    socketio: function() { 

    socket.on('chat', function (data) { 
     console.log(data.message); 
     console.log(data.user2); 
     this.setState({threads: data.message}); 
    }.bind(this)); 
    }, 
    _sendmessage: function() { 
     let message = this.refs.message2.value; 
     let User2 = this.refs.message1.value; 
     let Eml = LoginStore.getEmail(); 
    let chat = { 
     message: message, 
     user1: User1, 
     user2: User2, 
     emailusr1: Eml 
    } 
    console.log('Emiting ...'); 
    socket.emit('message', chat); 
    console.log('Done ...'); 

    }, 
    test: function(item1) { 
    var item = { 
     message: ':) hello :) hello :) hello :)' 
    } 
    console.log("smilies working"); 
    var texts = item1.message.split(/:\)/g); 
    console.log(texts); 
    var content = []; 
    for(var i = 0; i < texts.length - 1; i++) { 
     content.push(texts[i]); 
     content.push(<img src={Emojis[0].uri}/>); 
    } 
    return content.map(function(emoji) { 
     return (<span> {emoji} </span>) 
    }) 
    }, 
    render: function() { 
    return (
     <Paper zDepth={1} style={Sty1}> 
     {this.userlistio()} 
     {this.socketio()} 
     <CardTitle title="Threads" subtitle="" /> 
     <CardText> 
      Message threads 
      <div> 
      { 

      this.state.threads.map(function(item) { 

       return (<ListItem 
        leftAvatar={<Avatar src="profile pic" />} 
        primaryText={item.user1} 
        secondaryText={test({item})} 
        secondaryTextLines={2} 
       /> 
       ); 

      }) 
      } 
      </div> 
      <Paper style={Sty2} > 
       <input type="text" ref="message1" /> 
       <input type="text" ref="message2" /> 
      <input type="button" onClick={this._sendmessage} value="Send message" /> 
      </Paper> 



     </CardText> 
     <CardActions> 

     </CardActions> 
     </Paper> 
    ); 
    } 

}); 
+0

這個錯誤沒有與材料,UI組件做,相反,測試在您要調用的任何函數中都是未定義的。你可以發佈你的完整的組件,這段代碼片段居住在? –

+0

另外,一旦你正確地做了參考測試,你將會遇到一個問題,即你正在傳遞的參數。現在,測試需要一個具有消息屬性的對象 - 而不是通過它(從你有的組件片段)傳遞一個{item:item}對象 –

+0

test是組件上的一個屬性, - 你需要調用它與this.test –

回答

2

更改您的this.state.threads.map到:

//es6 
    this.state.threads.map(item => { //use arrow functions in es6 for this binding/readability 
     return (
      <ListItem 
      leftAvatar={<Avatar src="profile pic" />} 
      primaryText={item.user1} 
      secondaryText={this.test(item)} //changed item here to be the item as opposed to a new object of { item: item } as in your previous code. 
      secondaryTextLines={2} 
      /> 
    ); 
    }) 

    //es5 
    this.state.threads.map(function(item) { //use arrow functions in es6 for this binding/readability 
     return (
      <ListItem 
      leftAvatar={<Avatar src="profile pic" />} 
      primaryText={item.user1} 
      secondaryText={this.test(item)} //changed item here to be the item as opposed to a new object of { item: item } as in your previous code. 
      secondaryTextLines={2} 
      /> 
    ); 
    }.bind(this)) 
+1

瞭解它:)現在工作感謝分配 – TRomesh