Input value isn't updating in React











up vote
0
down vote

favorite












I have two files: App.js that's handling state and Counter.js. Everything gets displayed, accept onChange doesn't update the values in the input. What am I doing wrong in the Counter? Should I handle it differently than using props?



import React, { Component } from 'react';

import Counter from './components/Counter';
import './App.css';

class App extends Component {
state = {
cost: 0,
houseCost: 0,
downPayment: 0,
termOfLoan: 0,
annualInterestRate: 0
}
handleChange(e) {
this.setState({houseCost: e.target.houseCost});
}
handleCostChange = () => {
this.setState(
prevState => ({
cost: prevState.cost += 1
})
);
}
render() {
return (
<div className="App">
<Counter
cost={this.state.cost}
houseCost={this.state.houseCost}
downPayment={this.state.downPayment}
termOfLoan={this.state.termOfLoan}
annualInterestRate={this.state.annualInterestRate}
changeCost={this.handleCostChange}
handleChange={this.handleChange}
/>
</div>
);
}
}

export default App;


Counter.js



    import React from 'react';


const Counter = (props) => {

return (
<div className="counter">
<input type="text" value={props.houseCost} placeholder="House Cost" onChange={() => props.handleChange}></input>
<input type="text" value={props.downPayment} placeholder="Down Payment" onChange={() => props.handleChange}></input>
<input type="text" value={props.termOfLoan} placeholder="Mortgage Period (years)" onChange={() => props.handleChange}></input>
<input type="text" value={props.annualInterestRate} placeholder="Interest Rate" onChange={() => props.handleChange}></input>
<button className="counter-action" onClick={props.changeCost}>Calculate</button>
<span className="counter-score">{ props.cost }</span>
</div>
);
}


export default Counter;s









share|improve this question




















  • 1




    What's the desired behavior here? It's a bit unclear.
    – Colin
    Nov 5 at 18:11










  • Take the input House Cost - Right now it gets the value 0 from the state and doesn't get updated based on the input
    – Sonya T
    Nov 5 at 18:15

















up vote
0
down vote

favorite












I have two files: App.js that's handling state and Counter.js. Everything gets displayed, accept onChange doesn't update the values in the input. What am I doing wrong in the Counter? Should I handle it differently than using props?



import React, { Component } from 'react';

import Counter from './components/Counter';
import './App.css';

class App extends Component {
state = {
cost: 0,
houseCost: 0,
downPayment: 0,
termOfLoan: 0,
annualInterestRate: 0
}
handleChange(e) {
this.setState({houseCost: e.target.houseCost});
}
handleCostChange = () => {
this.setState(
prevState => ({
cost: prevState.cost += 1
})
);
}
render() {
return (
<div className="App">
<Counter
cost={this.state.cost}
houseCost={this.state.houseCost}
downPayment={this.state.downPayment}
termOfLoan={this.state.termOfLoan}
annualInterestRate={this.state.annualInterestRate}
changeCost={this.handleCostChange}
handleChange={this.handleChange}
/>
</div>
);
}
}

export default App;


Counter.js



    import React from 'react';


const Counter = (props) => {

return (
<div className="counter">
<input type="text" value={props.houseCost} placeholder="House Cost" onChange={() => props.handleChange}></input>
<input type="text" value={props.downPayment} placeholder="Down Payment" onChange={() => props.handleChange}></input>
<input type="text" value={props.termOfLoan} placeholder="Mortgage Period (years)" onChange={() => props.handleChange}></input>
<input type="text" value={props.annualInterestRate} placeholder="Interest Rate" onChange={() => props.handleChange}></input>
<button className="counter-action" onClick={props.changeCost}>Calculate</button>
<span className="counter-score">{ props.cost }</span>
</div>
);
}


export default Counter;s









share|improve this question




















  • 1




    What's the desired behavior here? It's a bit unclear.
    – Colin
    Nov 5 at 18:11










  • Take the input House Cost - Right now it gets the value 0 from the state and doesn't get updated based on the input
    – Sonya T
    Nov 5 at 18:15















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have two files: App.js that's handling state and Counter.js. Everything gets displayed, accept onChange doesn't update the values in the input. What am I doing wrong in the Counter? Should I handle it differently than using props?



import React, { Component } from 'react';

import Counter from './components/Counter';
import './App.css';

class App extends Component {
state = {
cost: 0,
houseCost: 0,
downPayment: 0,
termOfLoan: 0,
annualInterestRate: 0
}
handleChange(e) {
this.setState({houseCost: e.target.houseCost});
}
handleCostChange = () => {
this.setState(
prevState => ({
cost: prevState.cost += 1
})
);
}
render() {
return (
<div className="App">
<Counter
cost={this.state.cost}
houseCost={this.state.houseCost}
downPayment={this.state.downPayment}
termOfLoan={this.state.termOfLoan}
annualInterestRate={this.state.annualInterestRate}
changeCost={this.handleCostChange}
handleChange={this.handleChange}
/>
</div>
);
}
}

export default App;


Counter.js



    import React from 'react';


const Counter = (props) => {

return (
<div className="counter">
<input type="text" value={props.houseCost} placeholder="House Cost" onChange={() => props.handleChange}></input>
<input type="text" value={props.downPayment} placeholder="Down Payment" onChange={() => props.handleChange}></input>
<input type="text" value={props.termOfLoan} placeholder="Mortgage Period (years)" onChange={() => props.handleChange}></input>
<input type="text" value={props.annualInterestRate} placeholder="Interest Rate" onChange={() => props.handleChange}></input>
<button className="counter-action" onClick={props.changeCost}>Calculate</button>
<span className="counter-score">{ props.cost }</span>
</div>
);
}


export default Counter;s









share|improve this question















I have two files: App.js that's handling state and Counter.js. Everything gets displayed, accept onChange doesn't update the values in the input. What am I doing wrong in the Counter? Should I handle it differently than using props?



import React, { Component } from 'react';

import Counter from './components/Counter';
import './App.css';

class App extends Component {
state = {
cost: 0,
houseCost: 0,
downPayment: 0,
termOfLoan: 0,
annualInterestRate: 0
}
handleChange(e) {
this.setState({houseCost: e.target.houseCost});
}
handleCostChange = () => {
this.setState(
prevState => ({
cost: prevState.cost += 1
})
);
}
render() {
return (
<div className="App">
<Counter
cost={this.state.cost}
houseCost={this.state.houseCost}
downPayment={this.state.downPayment}
termOfLoan={this.state.termOfLoan}
annualInterestRate={this.state.annualInterestRate}
changeCost={this.handleCostChange}
handleChange={this.handleChange}
/>
</div>
);
}
}

export default App;


Counter.js



    import React from 'react';


const Counter = (props) => {

return (
<div className="counter">
<input type="text" value={props.houseCost} placeholder="House Cost" onChange={() => props.handleChange}></input>
<input type="text" value={props.downPayment} placeholder="Down Payment" onChange={() => props.handleChange}></input>
<input type="text" value={props.termOfLoan} placeholder="Mortgage Period (years)" onChange={() => props.handleChange}></input>
<input type="text" value={props.annualInterestRate} placeholder="Interest Rate" onChange={() => props.handleChange}></input>
<button className="counter-action" onClick={props.changeCost}>Calculate</button>
<span className="counter-score">{ props.cost }</span>
</div>
);
}


export default Counter;s






javascript reactjs react-props






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 5 at 18:21









Piyush Maurya

507516




507516










asked Nov 5 at 18:06









Sonya T

1141214




1141214








  • 1




    What's the desired behavior here? It's a bit unclear.
    – Colin
    Nov 5 at 18:11










  • Take the input House Cost - Right now it gets the value 0 from the state and doesn't get updated based on the input
    – Sonya T
    Nov 5 at 18:15
















  • 1




    What's the desired behavior here? It's a bit unclear.
    – Colin
    Nov 5 at 18:11










  • Take the input House Cost - Right now it gets the value 0 from the state and doesn't get updated based on the input
    – Sonya T
    Nov 5 at 18:15










1




1




What's the desired behavior here? It's a bit unclear.
– Colin
Nov 5 at 18:11




What's the desired behavior here? It's a bit unclear.
– Colin
Nov 5 at 18:11












Take the input House Cost - Right now it gets the value 0 from the state and doesn't get updated based on the input
– Sonya T
Nov 5 at 18:15






Take the input House Cost - Right now it gets the value 0 from the state and doesn't get updated based on the input
– Sonya T
Nov 5 at 18:15














3 Answers
3






active

oldest

votes

















up vote
4
down vote



accepted










change handleChange to an arrow function. It is not bound to the class so an arrow function will remove the context or you can bind the function in the constructor.



handleChange = (e, key) => {
this.setState({[key]: e.target.value});
}


this gives you a dynamic key so that you can use this handeChange function for all of your inputs and not have to write a new one.



also on your event in count.js isn't passing back the event.



onChange={(e) => props.handleChange(e, 'houseCost')}





share|improve this answer























  • You're close, you missed my implementation slightly: {[key]: e.target.houseCost});
    – Ted
    Nov 5 at 18:21






  • 1




    Thanks @Ted I like your dynamic use of key
    – Mike
    Nov 5 at 18:23












  • Please comment any way you think this answer can improve, or even submit an edit.
    – Mike
    Nov 5 at 19:00


















up vote
4
down vote













the issue is in your onChange handler. When you call this method you change the state only of one value "houseCost" and finally, the expected "e.target.value" is not correct. Try something like this:



this.setState({ [e.target.name]: e.target.value });


And for each input like:



 <input type="text" name="houseCost" value={props.houseCost} placeholder="House Cost" onChange={() => props.handleChange} />


So, the input attribute "name" allows you to specify "event target name" and use them to change specific state value by "event target value" by one handler






share|improve this answer










New contributor




Eugene Dzhevadov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    up vote
    2
    down vote













    You've got a couple problems, both in the handleChange function, and the way it's called from counter. See the example below:






    class App extends React.Component {
    state = {
    cost: 0,
    houseCost: 0,
    downPayment: 0,
    termOfLoan: 0,
    annualInterestRate: 0
    }
    handleChange = (e, key) => {
    this.setState({[key]: e.target.value});
    }
    handleCostChange = () => {
    this.setState(
    prevState => ({
    cost: prevState.cost += 1
    })
    );
    }
    render() {
    return (
    <div className="App">
    <Counter
    cost={this.state.cost}
    houseCost={this.state.houseCost}
    downPayment={this.state.downPayment}
    termOfLoan={this.state.termOfLoan}
    annualInterestRate={this.state.annualInterestRate}
    changeCost={this.handleCostChange}
    handleChange={this.handleChange}
    />
    </div>
    );
    }
    }




    const Counter = (props) => (
    <div className="counter">
    <input
    type="text"
    value={props.houseCost}
    placeholder="House Cost"
    onChange={e => props.handleChange(e, 'houseCost')}
    />
    <input
    type="text"
    value={props.downPayment}
    placeholder="Down Payment"
    onChange={e => props.handleChange(e, 'downPayment')}
    />
    <input
    type="text"
    value={props.termOfLoan}
    placeholder="Mortgage Period (years)"
    onChange={e => props.handleChange(e, 'termOfLoan')}
    />
    <input
    type="text"
    value={props.annualInterestRate}
    placeholder="Interest Rate"
    onChange={e => props.handleChange(e, 'annualInterestRate')}
    />
    <button className="counter-action" onClick={props.changeCost}>Calculate</button>
    <span className="counter-score">{ props.cost }</span>
    </div>
    );



    ReactDOM.render(
    <App />,
    document.getElementById("react")
    );

    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
    <div id="react"></div>








    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53159846%2finput-value-isnt-updating-in-react%23new-answer', 'question_page');
      }
      );

      Post as a guest
































      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      4
      down vote



      accepted










      change handleChange to an arrow function. It is not bound to the class so an arrow function will remove the context or you can bind the function in the constructor.



      handleChange = (e, key) => {
      this.setState({[key]: e.target.value});
      }


      this gives you a dynamic key so that you can use this handeChange function for all of your inputs and not have to write a new one.



      also on your event in count.js isn't passing back the event.



      onChange={(e) => props.handleChange(e, 'houseCost')}





      share|improve this answer























      • You're close, you missed my implementation slightly: {[key]: e.target.houseCost});
        – Ted
        Nov 5 at 18:21






      • 1




        Thanks @Ted I like your dynamic use of key
        – Mike
        Nov 5 at 18:23












      • Please comment any way you think this answer can improve, or even submit an edit.
        – Mike
        Nov 5 at 19:00















      up vote
      4
      down vote



      accepted










      change handleChange to an arrow function. It is not bound to the class so an arrow function will remove the context or you can bind the function in the constructor.



      handleChange = (e, key) => {
      this.setState({[key]: e.target.value});
      }


      this gives you a dynamic key so that you can use this handeChange function for all of your inputs and not have to write a new one.



      also on your event in count.js isn't passing back the event.



      onChange={(e) => props.handleChange(e, 'houseCost')}





      share|improve this answer























      • You're close, you missed my implementation slightly: {[key]: e.target.houseCost});
        – Ted
        Nov 5 at 18:21






      • 1




        Thanks @Ted I like your dynamic use of key
        – Mike
        Nov 5 at 18:23












      • Please comment any way you think this answer can improve, or even submit an edit.
        – Mike
        Nov 5 at 19:00













      up vote
      4
      down vote



      accepted







      up vote
      4
      down vote



      accepted






      change handleChange to an arrow function. It is not bound to the class so an arrow function will remove the context or you can bind the function in the constructor.



      handleChange = (e, key) => {
      this.setState({[key]: e.target.value});
      }


      this gives you a dynamic key so that you can use this handeChange function for all of your inputs and not have to write a new one.



      also on your event in count.js isn't passing back the event.



      onChange={(e) => props.handleChange(e, 'houseCost')}





      share|improve this answer














      change handleChange to an arrow function. It is not bound to the class so an arrow function will remove the context or you can bind the function in the constructor.



      handleChange = (e, key) => {
      this.setState({[key]: e.target.value});
      }


      this gives you a dynamic key so that you can use this handeChange function for all of your inputs and not have to write a new one.



      also on your event in count.js isn't passing back the event.



      onChange={(e) => props.handleChange(e, 'houseCost')}






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 5 at 18:28

























      answered Nov 5 at 18:10









      Mike

      764




      764












      • You're close, you missed my implementation slightly: {[key]: e.target.houseCost});
        – Ted
        Nov 5 at 18:21






      • 1




        Thanks @Ted I like your dynamic use of key
        – Mike
        Nov 5 at 18:23












      • Please comment any way you think this answer can improve, or even submit an edit.
        – Mike
        Nov 5 at 19:00


















      • You're close, you missed my implementation slightly: {[key]: e.target.houseCost});
        – Ted
        Nov 5 at 18:21






      • 1




        Thanks @Ted I like your dynamic use of key
        – Mike
        Nov 5 at 18:23












      • Please comment any way you think this answer can improve, or even submit an edit.
        – Mike
        Nov 5 at 19:00
















      You're close, you missed my implementation slightly: {[key]: e.target.houseCost});
      – Ted
      Nov 5 at 18:21




      You're close, you missed my implementation slightly: {[key]: e.target.houseCost});
      – Ted
      Nov 5 at 18:21




      1




      1




      Thanks @Ted I like your dynamic use of key
      – Mike
      Nov 5 at 18:23






      Thanks @Ted I like your dynamic use of key
      – Mike
      Nov 5 at 18:23














      Please comment any way you think this answer can improve, or even submit an edit.
      – Mike
      Nov 5 at 19:00




      Please comment any way you think this answer can improve, or even submit an edit.
      – Mike
      Nov 5 at 19:00












      up vote
      4
      down vote













      the issue is in your onChange handler. When you call this method you change the state only of one value "houseCost" and finally, the expected "e.target.value" is not correct. Try something like this:



      this.setState({ [e.target.name]: e.target.value });


      And for each input like:



       <input type="text" name="houseCost" value={props.houseCost} placeholder="House Cost" onChange={() => props.handleChange} />


      So, the input attribute "name" allows you to specify "event target name" and use them to change specific state value by "event target value" by one handler






      share|improve this answer










      New contributor




      Eugene Dzhevadov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















        up vote
        4
        down vote













        the issue is in your onChange handler. When you call this method you change the state only of one value "houseCost" and finally, the expected "e.target.value" is not correct. Try something like this:



        this.setState({ [e.target.name]: e.target.value });


        And for each input like:



         <input type="text" name="houseCost" value={props.houseCost} placeholder="House Cost" onChange={() => props.handleChange} />


        So, the input attribute "name" allows you to specify "event target name" and use them to change specific state value by "event target value" by one handler






        share|improve this answer










        New contributor




        Eugene Dzhevadov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.




















          up vote
          4
          down vote










          up vote
          4
          down vote









          the issue is in your onChange handler. When you call this method you change the state only of one value "houseCost" and finally, the expected "e.target.value" is not correct. Try something like this:



          this.setState({ [e.target.name]: e.target.value });


          And for each input like:



           <input type="text" name="houseCost" value={props.houseCost} placeholder="House Cost" onChange={() => props.handleChange} />


          So, the input attribute "name" allows you to specify "event target name" and use them to change specific state value by "event target value" by one handler






          share|improve this answer










          New contributor




          Eugene Dzhevadov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          the issue is in your onChange handler. When you call this method you change the state only of one value "houseCost" and finally, the expected "e.target.value" is not correct. Try something like this:



          this.setState({ [e.target.name]: e.target.value });


          And for each input like:



           <input type="text" name="houseCost" value={props.houseCost} placeholder="House Cost" onChange={() => props.handleChange} />


          So, the input attribute "name" allows you to specify "event target name" and use them to change specific state value by "event target value" by one handler







          share|improve this answer










          New contributor




          Eugene Dzhevadov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer








          edited Nov 5 at 18:26





















          New contributor




          Eugene Dzhevadov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered Nov 5 at 18:18









          Eugene Dzhevadov

          863




          863




          New contributor




          Eugene Dzhevadov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          Eugene Dzhevadov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          Eugene Dzhevadov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






















              up vote
              2
              down vote













              You've got a couple problems, both in the handleChange function, and the way it's called from counter. See the example below:






              class App extends React.Component {
              state = {
              cost: 0,
              houseCost: 0,
              downPayment: 0,
              termOfLoan: 0,
              annualInterestRate: 0
              }
              handleChange = (e, key) => {
              this.setState({[key]: e.target.value});
              }
              handleCostChange = () => {
              this.setState(
              prevState => ({
              cost: prevState.cost += 1
              })
              );
              }
              render() {
              return (
              <div className="App">
              <Counter
              cost={this.state.cost}
              houseCost={this.state.houseCost}
              downPayment={this.state.downPayment}
              termOfLoan={this.state.termOfLoan}
              annualInterestRate={this.state.annualInterestRate}
              changeCost={this.handleCostChange}
              handleChange={this.handleChange}
              />
              </div>
              );
              }
              }




              const Counter = (props) => (
              <div className="counter">
              <input
              type="text"
              value={props.houseCost}
              placeholder="House Cost"
              onChange={e => props.handleChange(e, 'houseCost')}
              />
              <input
              type="text"
              value={props.downPayment}
              placeholder="Down Payment"
              onChange={e => props.handleChange(e, 'downPayment')}
              />
              <input
              type="text"
              value={props.termOfLoan}
              placeholder="Mortgage Period (years)"
              onChange={e => props.handleChange(e, 'termOfLoan')}
              />
              <input
              type="text"
              value={props.annualInterestRate}
              placeholder="Interest Rate"
              onChange={e => props.handleChange(e, 'annualInterestRate')}
              />
              <button className="counter-action" onClick={props.changeCost}>Calculate</button>
              <span className="counter-score">{ props.cost }</span>
              </div>
              );



              ReactDOM.render(
              <App />,
              document.getElementById("react")
              );

              <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
              <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
              <div id="react"></div>








              share|improve this answer

























                up vote
                2
                down vote













                You've got a couple problems, both in the handleChange function, and the way it's called from counter. See the example below:






                class App extends React.Component {
                state = {
                cost: 0,
                houseCost: 0,
                downPayment: 0,
                termOfLoan: 0,
                annualInterestRate: 0
                }
                handleChange = (e, key) => {
                this.setState({[key]: e.target.value});
                }
                handleCostChange = () => {
                this.setState(
                prevState => ({
                cost: prevState.cost += 1
                })
                );
                }
                render() {
                return (
                <div className="App">
                <Counter
                cost={this.state.cost}
                houseCost={this.state.houseCost}
                downPayment={this.state.downPayment}
                termOfLoan={this.state.termOfLoan}
                annualInterestRate={this.state.annualInterestRate}
                changeCost={this.handleCostChange}
                handleChange={this.handleChange}
                />
                </div>
                );
                }
                }




                const Counter = (props) => (
                <div className="counter">
                <input
                type="text"
                value={props.houseCost}
                placeholder="House Cost"
                onChange={e => props.handleChange(e, 'houseCost')}
                />
                <input
                type="text"
                value={props.downPayment}
                placeholder="Down Payment"
                onChange={e => props.handleChange(e, 'downPayment')}
                />
                <input
                type="text"
                value={props.termOfLoan}
                placeholder="Mortgage Period (years)"
                onChange={e => props.handleChange(e, 'termOfLoan')}
                />
                <input
                type="text"
                value={props.annualInterestRate}
                placeholder="Interest Rate"
                onChange={e => props.handleChange(e, 'annualInterestRate')}
                />
                <button className="counter-action" onClick={props.changeCost}>Calculate</button>
                <span className="counter-score">{ props.cost }</span>
                </div>
                );



                ReactDOM.render(
                <App />,
                document.getElementById("react")
                );

                <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
                <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
                <div id="react"></div>








                share|improve this answer























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  You've got a couple problems, both in the handleChange function, and the way it's called from counter. See the example below:






                  class App extends React.Component {
                  state = {
                  cost: 0,
                  houseCost: 0,
                  downPayment: 0,
                  termOfLoan: 0,
                  annualInterestRate: 0
                  }
                  handleChange = (e, key) => {
                  this.setState({[key]: e.target.value});
                  }
                  handleCostChange = () => {
                  this.setState(
                  prevState => ({
                  cost: prevState.cost += 1
                  })
                  );
                  }
                  render() {
                  return (
                  <div className="App">
                  <Counter
                  cost={this.state.cost}
                  houseCost={this.state.houseCost}
                  downPayment={this.state.downPayment}
                  termOfLoan={this.state.termOfLoan}
                  annualInterestRate={this.state.annualInterestRate}
                  changeCost={this.handleCostChange}
                  handleChange={this.handleChange}
                  />
                  </div>
                  );
                  }
                  }




                  const Counter = (props) => (
                  <div className="counter">
                  <input
                  type="text"
                  value={props.houseCost}
                  placeholder="House Cost"
                  onChange={e => props.handleChange(e, 'houseCost')}
                  />
                  <input
                  type="text"
                  value={props.downPayment}
                  placeholder="Down Payment"
                  onChange={e => props.handleChange(e, 'downPayment')}
                  />
                  <input
                  type="text"
                  value={props.termOfLoan}
                  placeholder="Mortgage Period (years)"
                  onChange={e => props.handleChange(e, 'termOfLoan')}
                  />
                  <input
                  type="text"
                  value={props.annualInterestRate}
                  placeholder="Interest Rate"
                  onChange={e => props.handleChange(e, 'annualInterestRate')}
                  />
                  <button className="counter-action" onClick={props.changeCost}>Calculate</button>
                  <span className="counter-score">{ props.cost }</span>
                  </div>
                  );



                  ReactDOM.render(
                  <App />,
                  document.getElementById("react")
                  );

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
                  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
                  <div id="react"></div>








                  share|improve this answer












                  You've got a couple problems, both in the handleChange function, and the way it's called from counter. See the example below:






                  class App extends React.Component {
                  state = {
                  cost: 0,
                  houseCost: 0,
                  downPayment: 0,
                  termOfLoan: 0,
                  annualInterestRate: 0
                  }
                  handleChange = (e, key) => {
                  this.setState({[key]: e.target.value});
                  }
                  handleCostChange = () => {
                  this.setState(
                  prevState => ({
                  cost: prevState.cost += 1
                  })
                  );
                  }
                  render() {
                  return (
                  <div className="App">
                  <Counter
                  cost={this.state.cost}
                  houseCost={this.state.houseCost}
                  downPayment={this.state.downPayment}
                  termOfLoan={this.state.termOfLoan}
                  annualInterestRate={this.state.annualInterestRate}
                  changeCost={this.handleCostChange}
                  handleChange={this.handleChange}
                  />
                  </div>
                  );
                  }
                  }




                  const Counter = (props) => (
                  <div className="counter">
                  <input
                  type="text"
                  value={props.houseCost}
                  placeholder="House Cost"
                  onChange={e => props.handleChange(e, 'houseCost')}
                  />
                  <input
                  type="text"
                  value={props.downPayment}
                  placeholder="Down Payment"
                  onChange={e => props.handleChange(e, 'downPayment')}
                  />
                  <input
                  type="text"
                  value={props.termOfLoan}
                  placeholder="Mortgage Period (years)"
                  onChange={e => props.handleChange(e, 'termOfLoan')}
                  />
                  <input
                  type="text"
                  value={props.annualInterestRate}
                  placeholder="Interest Rate"
                  onChange={e => props.handleChange(e, 'annualInterestRate')}
                  />
                  <button className="counter-action" onClick={props.changeCost}>Calculate</button>
                  <span className="counter-score">{ props.cost }</span>
                  </div>
                  );



                  ReactDOM.render(
                  <App />,
                  document.getElementById("react")
                  );

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
                  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
                  <div id="react"></div>








                  class App extends React.Component {
                  state = {
                  cost: 0,
                  houseCost: 0,
                  downPayment: 0,
                  termOfLoan: 0,
                  annualInterestRate: 0
                  }
                  handleChange = (e, key) => {
                  this.setState({[key]: e.target.value});
                  }
                  handleCostChange = () => {
                  this.setState(
                  prevState => ({
                  cost: prevState.cost += 1
                  })
                  );
                  }
                  render() {
                  return (
                  <div className="App">
                  <Counter
                  cost={this.state.cost}
                  houseCost={this.state.houseCost}
                  downPayment={this.state.downPayment}
                  termOfLoan={this.state.termOfLoan}
                  annualInterestRate={this.state.annualInterestRate}
                  changeCost={this.handleCostChange}
                  handleChange={this.handleChange}
                  />
                  </div>
                  );
                  }
                  }




                  const Counter = (props) => (
                  <div className="counter">
                  <input
                  type="text"
                  value={props.houseCost}
                  placeholder="House Cost"
                  onChange={e => props.handleChange(e, 'houseCost')}
                  />
                  <input
                  type="text"
                  value={props.downPayment}
                  placeholder="Down Payment"
                  onChange={e => props.handleChange(e, 'downPayment')}
                  />
                  <input
                  type="text"
                  value={props.termOfLoan}
                  placeholder="Mortgage Period (years)"
                  onChange={e => props.handleChange(e, 'termOfLoan')}
                  />
                  <input
                  type="text"
                  value={props.annualInterestRate}
                  placeholder="Interest Rate"
                  onChange={e => props.handleChange(e, 'annualInterestRate')}
                  />
                  <button className="counter-action" onClick={props.changeCost}>Calculate</button>
                  <span className="counter-score">{ props.cost }</span>
                  </div>
                  );



                  ReactDOM.render(
                  <App />,
                  document.getElementById("react")
                  );

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
                  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
                  <div id="react"></div>





                  class App extends React.Component {
                  state = {
                  cost: 0,
                  houseCost: 0,
                  downPayment: 0,
                  termOfLoan: 0,
                  annualInterestRate: 0
                  }
                  handleChange = (e, key) => {
                  this.setState({[key]: e.target.value});
                  }
                  handleCostChange = () => {
                  this.setState(
                  prevState => ({
                  cost: prevState.cost += 1
                  })
                  );
                  }
                  render() {
                  return (
                  <div className="App">
                  <Counter
                  cost={this.state.cost}
                  houseCost={this.state.houseCost}
                  downPayment={this.state.downPayment}
                  termOfLoan={this.state.termOfLoan}
                  annualInterestRate={this.state.annualInterestRate}
                  changeCost={this.handleCostChange}
                  handleChange={this.handleChange}
                  />
                  </div>
                  );
                  }
                  }




                  const Counter = (props) => (
                  <div className="counter">
                  <input
                  type="text"
                  value={props.houseCost}
                  placeholder="House Cost"
                  onChange={e => props.handleChange(e, 'houseCost')}
                  />
                  <input
                  type="text"
                  value={props.downPayment}
                  placeholder="Down Payment"
                  onChange={e => props.handleChange(e, 'downPayment')}
                  />
                  <input
                  type="text"
                  value={props.termOfLoan}
                  placeholder="Mortgage Period (years)"
                  onChange={e => props.handleChange(e, 'termOfLoan')}
                  />
                  <input
                  type="text"
                  value={props.annualInterestRate}
                  placeholder="Interest Rate"
                  onChange={e => props.handleChange(e, 'annualInterestRate')}
                  />
                  <button className="counter-action" onClick={props.changeCost}>Calculate</button>
                  <span className="counter-score">{ props.cost }</span>
                  </div>
                  );



                  ReactDOM.render(
                  <App />,
                  document.getElementById("react")
                  );

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
                  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
                  <div id="react"></div>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 5 at 18:17









                  Ted

                  10.5k11942




                  10.5k11942






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53159846%2finput-value-isnt-updating-in-react%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest




















































































                      這個網誌中的熱門文章

                      Hercules Kyvelos

                      Tangent Lines Diagram Along Smooth Curve

                      Yusuf al-Mu'taman ibn Hud