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
javascript reactjs react-props
add a comment |
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
javascript reactjs react-props
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
add a comment |
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
javascript reactjs react-props
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
javascript reactjs react-props
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
add a comment |
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
add a comment |
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')}
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
add a comment |
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
New contributor
add a comment |
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>
add a comment |
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')}
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
add a comment |
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')}
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
add a comment |
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')}
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')}
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
add a comment |
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
add a comment |
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
New contributor
add a comment |
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
New contributor
add a comment |
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
New contributor
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
New contributor
edited Nov 5 at 18:26
New contributor
answered Nov 5 at 18:18
Eugene Dzhevadov
863
863
New contributor
New contributor
add a comment |
add a comment |
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>
add a comment |
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>
add a comment |
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>
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>
answered Nov 5 at 18:17
Ted
10.5k11942
10.5k11942
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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