I'm a beginner in react.js and I'm working on a small project right now. Here what I want to do is, once you double click on a text, it changes to a text box and allows you to enter some text. If you click on 'X', it displays the initial value and if you click on 'OK', it displays the edited value.
But I have a small problem. When I navigate back to the page after navigating to another, the value changes to the initial state. But I want to keep the edited value until I change it, not the initial value. How can I do that? Any help would be appreciated.
render() {
return (
<tr>
<td>
{this.state.isInEditMode ? this.editView() : this.defaultView()}
</td>
<tr>
);
}
state = {
value:"Some Text Here",
isInEditMode:false
}
changeEditMode = () =>{
this.setState({
isInEditMode:!this.state.isInEditMode
})
}
updateComponentValue = () =>{
this.setState({
isInEditMode:false,
value:this.refs.theTextInput.value
})
}
editView = () =>{
return <div>
<input type="text" defaultValue={this.state.value} ref="theTextInput"/>
<button onClick={this.changeEditMode}>X</button>
<button onClick={this.updateComponentValue}>OK</button>
</div>
}
defaultView = () =>{
return <div onDoubleClick={this.changeEditMode}>{this.state.value}</div>
}
ref :https://www.youtube.com/watch?v=WTh54FMNrbU
This is my App.js file.
import React, { Component } from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
import { BrowserRouter as Router, Switch, Route, Link} from 'react-router-dom';
import deliveryDetails from './components/deliveryDetails.component';
import Index from './components/index.component';
class App extends Component {
render() {
return (
<Router>
<div className="container">
<nav className="navbar navbar-expand-lg navbar-light bg-light">
<Link to={'/'} className="navbar-brand">Payment</Link>
<div className="collapse navbar-collapse" id="navbarSupportedContent">
<ul className="navbar-nav mr-auto">
<li className="nav-item">
<Link to={'/create'} className="nav-link">Create</Link>
</li>
<li className="nav-item">
<Link to={'/index'} className="nav-link">Index</Link>
</li>
</ul>
</div>
</nav> <br/>
<Switch>
<Route exact path='/create' component={ deliveryDetails } />
<Route path='/index' component={ Index } />
</Switch>
</div>
</Router>
);
}
}
export default App;
The above edittable text is in index route. It's value changes to default when I navigate back to index route from create route.