我是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