Don't refresh the input values on component change

to don something
don urban dictionary
don synonym
don medical abbreviation
don meaning slang
don and doff
don meaning italian
don japanese

When I enter the input fields and switch to another component, previously entered values get refreshed.

import React from "react";

const Home = () => (
  <div>
    <h2>Home</h2>
    <input type="text" />
  </div>
);

export default Home;

How to prevent the values from getting refreshed.

Demo: https://codesandbox.io/s/jv7mk6596v

Here you have an uncontrolled component (the <input>). The value of the input is only stored in the DOM, and when it's unmounted, the value is gone.

To make the value persist, we need to make it a controlled component and keep the value of the input at an appropriate level. If you want it to persist longer than the instance of the Home component (and assuming you only have one Home component), you can keep the input value in the parent component of Home. E.g.:

import React from "react";

const Home = (props) => (
  <div>
    <h2>Home</h2>
    <input
      type="text"
      value={props.value}
      onChange={event => props.onChange(event.target.value)}
    />
  </div>
);

class App extends React.Component {
  state = {
    home: {
      value: ""
    }
  }
  render() {
    return (
      <div>
        <Home
          value={this.state.home.value}
          onChange={value => this.setState({ home: { value } })}
        />
      </div>
    )
  }
}

Note that this allows the value to persist even if you stop rendering the Home component and replace it with a new one, exactly because the state is not stored in the Home component itself.

Further, as an alternative to keeping the state in a component, you can also use a store (created with e.g. Redux), but the principle is the same: If you want the value to persist longer than the component instance, then you must save it outside the component instance – and also, outside the elements rendered by the component instance, as you have done. However, whether you keep the state in a store or in a more long-lived component is more of an implementation detail.

Edit

I have looked at your code example, and in your case it will probably be cleaner to use a store. However, I'll just show you an example of how you can solve it with component state. Here is your project with the necessary changes: https://codesandbox.io/s/6vqz30jjon

Don, of address, especially for a priest. a person of great importance. 1. Don (also dōn) Used as a courtesy title before the name of a man in a Spanish-speaking area. 2.

Integrated react app with redux.

Redux is a predictable state container for JavaScript apps. Redux makes it easy to manage the state of your application

Install related npm packages and follow this link

don, Definition of don. (Entry 1 of 4) transitive verb. 1 : to put on (an article of clothing) donned his hat and gloves. 2 : to wrap oneself in : take on sense 3a the donning of new and more tyrannous moralities— Edward Sapir. a song or ode in praise or honor of God, a deity, a nation, etc. a style of popular music for dancing, usually recorded and with complex electronic instrumentation, in which simple, repetitive lyrics are subordinated to a heavy, pulsating, rhythmic beat.

Here is my solution, using HOC and localStorage API to persist your data.

Donn (given name), A don is a guy that everyone wants to be like. He is not only sexy and muscular but amazing in bed aswell. He can rock anyone. Being a don is a talent that not  Don, and dom, is derived from the Latin Dominus: a master of a household, a title with background from the Roman Republic in classical antiquity.

Don, verb (used with object), donned, don·ning. to put on or dress in: to don one's clothes. Origin of  Dôn, in Celtic mythology, leader of one of two warring families of gods; according to one interpretation, the Children of Dôn were the powers of light, constantly in conflict with the Children of Llyr, the powers of darkness. In another view, the conflict was a struggle between indigenous gods and those of an invading people.

Don, VerbEdit. don (third-person singular simple present dons, present participle donning, simple past and past participle donned). (  Edward Don and Company is the world's leading distributor of foodservice equipment and supplies. Online ordering

don, To don means to put on, as in clothing or hats. A hunter will don his camouflage clothes when he goes hunting. A don is a guy that everyone wants to be like. He is not only sexy and muscular but amazing in bed aswell. He can rock anyone. Being a don is a talent that not everyone can achieve.

Comments
  • Use stores, storage, db
  • @Alexis How can achieve this with stores. could you please give me a example
  • Use controlled components i.e. add value and onChange props to input. Store the value outside the component e.g. with redux
  • @MariaJeysinghAnbu Learn about Redux. I don't have time to code this for you it's better for you to try it by yourself. redux.js.org/introduction/getting-started