我是JS和React的新手,我正在開發一個訓練營項目。我在聊天應用程序上進行協作,我想了解如何用變量替換字符串以清理代碼。這裏就是我的工作:使用變量代替字符串
import React from 'react';
const Form = React.createClass({
submit(e) {
e.preventDefault();
this.props.messagesRef.push({
text: this.refs.text.value,
time: Date.now(),
user: {
displayName: this.props.user.displayName,
photoURL: this.props.user.photoURL,
uid: this.props.user.uid,
},
});
this.refs.text.value = '';
},
render() {
return (
<form className="form" onSubmit={this.submit}>
<input className="form-input" placeholder="Write something…" ref="text"/>
<button className="form-button">Send</button>
</form>
);
}
});
export default Form;
我想用一個變量來代替this.refs.text.value
這樣我就可以清理代碼,但我真的不知道怎麼樣。任何幫助將不勝感激
'this.ref.text.value'有什麼問題? – Li357
有沒有問題,我只想學習如何用變量替換它 –
你的意思是這樣的:'var val = this.ref.text.value;''?然後:'text:val' – Li357