我正在嘗試編輯和更新React中的一些文本。我正在嘗試使用提示符,但我不確定這是否正確。當我console.log this.state.newText
沒有任何顯示。有更好的方法去更新文本?我在我的HTML中使用表格,因此在其中使用輸入會導致錯誤。謝謝!在React中編輯文本
import React, { Component } from 'react';
import {IndexLink} from 'react-router';
import Item from '../styles/LinkItem.css';
class LinkItem extends Component {
constructor(props) {
super(props);
this.state = {
clicks: 0,
text: this.props.link,
newText: '',
editing: false
};
}
}
editLink() {
var newText = prompt('Update your link');
this.setState({
newText: newText,
editing: false
});
console.log(this.state.newText);
}
handleClick() {
const clicks = this.state.clicks;
this.setState({
clicks: clicks + 1
});
localStorage.setItem(`link-${this.props.link}`, JSON.stringify(clicks));
}
render() {
return (
<tr>
<td>
<IndexLink onClick={this.handleClick.bind(this)} to={{pathname: 'landing/' + this.props.link}}>{this.props.link}</IndexLink>
</td>
<td>{JSON.parse(localStorage.getItem(`link-${this.props.link}`))}</td>
<td><button className="btn btn-default" onClick={this.editLink.bind(this)}>Edit</button></td>
<td><button className="btn btn-danger" onClick={this.props.data.deleteLink.bind(null, this.props.index)}>Delete</button></td>
</tr>
);
}
}
export default LinkItem;