How to get values from select react js - reactjs

class button extends Component {
constructor(props) {
super(props);
this.state = {
role: ''
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({ [event.target.id]: event.target.value });
}
handleSubmit(event) {
console.log(this.state.role);
event.preventDefault();
}
render() {
return (
<div>
<form onSubmit={this.handleSubmit}>
<label>
<select value={this.state.role} onChange={this.handleChange}>
<option value="A" >A</option>
<option value="B" >B</option>
</select>
</label>
<input type="submit" value="a" />
</form>
</div>
);
}
}
If I were to go to the scroll bar and click either A or B it should console log A or B but it gives empty. Why? At the moment I can't scroll the bar either. I tried using name, id as this.state.role those didn't work either

You are using the event.target.id in your handleChange method, but your select has no id property (btw, property "name" will be better suited than "id" for this). Also, you should define an initial role value (as your select is set to A, by default);
class Button extends React.Component {
constructor(props) {
super(props);
this.state = {
role: 'A'
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({
[event.target.id]: event.target.value
});
}
handleSubmit(event) {
console.log(this.state.role);
event.preventDefault();
}
render() {
return (<div>
<form onSubmit={this.handleSubmit}>
<label>
<select id="role" value={this.state.role} onChange={this.handleChange}>
<option value="A">A</option>
<option value="B">B</option>
</select>
</label>
<input type="submit" value="a"/>
</form>
</div>);
}
}
ReactDOM.render(< Button / >, document.getElementById('root'));
<script crossorigin src="https://unpkg.com/react#16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom#16/umd/react-dom.development.js"></script>
<div id="root"></div>

Try code below
class button extends Component {
constructor(props) {
super(props);
this.state = {
role: ''
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({ [event.target.name]: event.target.value });
}
handleSubmit(event) {
console.log(this.state.role);
event.preventDefault();
}
render() {
return (
<div>
<form onSubmit={this.handleSubmit}>
<label>
<select name="role" value={this.state.role} onChange={this.handleChange}>
<option value="A" >A</option>
<option value="B" >B</option>
</select>
</label>
<input type="submit" value="a" />
</form>
</div>
);
}
}
Also, check the documentation on controlled components and how to handle forms in react https://reactjs.org/docs/forms.html

If i understood you correctly this is the solution to your problem:
class Button extends React.Component {
constructor(props) {
super(props);
this.state = {
role: 'A'
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
console.log('####onChange Value: ', event.target.value)
this.setState({ role: event.target.value });
}
handleSubmit(event) {
console.log('submited value: ',this.state.role);
event.preventDefault();
}
render() {
return (
<div>
<form onSubmit={this.handleSubmit}>
<label>
<select value={this.state.role} onChange={this.handleChange}>
<option value="A" >A</option>
<option value="B" >B</option>
</select>
</label>
<input type="submit" value={this.state.role} />
</form>
</div>
);
}
}

Related

how to set state from on change for multiple events react

in my react form I need to save each data item into the state.
I currently have on change methods for each of the inputs but its a lot of very similar code and looks messy. (But it does work...)
Here is the code
class EnterMortgage extends React.Component {
constructor(props) {
super(props);
this.state = {
repaymentType: '',
propVal: '',
bal: '',
fullTerm: '',
remainTerm: '',
intRate: '',
};
this.handleRepaymentChange = this.handleRepaymentChange.bind(this);
this.handlePropValChange = this.handlePropValChange.bind(this);
this.handleBalChange = this.handleBalChange.bind(this);
this.handleFullTermChange = this.handleFullTermChange.bind(this);
this.handleRemainTermChange = this.handleRemainTermChange.bind(this);
this.handleIntRateChange = this.handleIntRateChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleRepaymentChange(event) {
this.setState({ repaymentType: event.target.value });
}
handlePropValChange(event) {
this.setState({ propVal: event.target.value });
}
handleBalChange(event) {
this.setState({ bal: event.target.value });
}
handleFullTermChange(event) {
this.setState({ fullTerm: event.target.value });
}
handleRemainTermChange(event) {
this.setState({ remainTerm: event.target.value });=
}
handleIntRateChange(event) {
this.setState({ intRate: event.target.value });
}
handleSubmit(event) {
this.props.history.push('/EnterSavings', this.state);
}
// renders to display on page
render() {
return (
<div>
<div >
<p> Enter your mortgage details </p>
</div>
<div>
<form onSubmit={this.handleSubmit}>
<label>
Property Value {'\u00A3'}
<input type="Number" name="propVal" onChange={this.handlePropValChange} />
</label>
<label>
Current Balance
<input type="Number" name="bal" onChange={this.handleBalChange}/>
</label>
<label>
Full Mortgage Term (months)
<input type="Number" name="fullTerm" onChange={this.handleFullTermChange} />
</label>
<label>
Remaining Mortgage Term (months)
<input type="Number" name="remainTerm" onChange={this.handleRemainTermChange} />
</label>
<label>
InterestRate
<input type="Number" name="intRate" onChange={this.handleIntRateChange} />
</label>
<label>
Repayment Method
<select onChange={this.handleRepaymentChange}>
<option value="repayment">Repayment</option>
<option value="interest">Interest Only</option>
<option value="pap">Part and Part</option>
</select>
</label>
<input type="submit" value="Submit" />
</form>
</div>
</div>
);
}
}
export default EnterMortgage;
Is there a way to refactor this rather than having multiple functions? I have tried to combined into one method but I couldn't manage to get each item updated.

Handling events of forms

I am new to React and I am trying to make a form I wrote this simple form
class Profile extends React.Component {
constructor(props) {
super(props);
this.state = {name: '', age:''};
this.handleNameChange = this.handleNameChange.bind(this);
this.handleAgeChange = this.handleAgeChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleNameChange(event) {
this.setState({name: event.target.name});
}
handleAgeChange(event) {
this.setState({age: event.target.age});
}
handleSubmit(event) {
alert('Name was submitted: ' + this.state.value);
alert('Age was submitted: '+ this.state.age);
event.preventDefault();
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Name:
<input type="text" value={this.state.name} onChange={this.handleNameChange} />
</label>
<label>
Age:
<input type="text" value={this.state.age} onChange={this.handleAgeChange}/>
</label>
<input type="submit" value="Submit" />
</form>
);
}
}
export default Profile;
When I try it I can't write anything in the Name and the alert always shows me 'Name was submitted: undefined' 'Age was submitted: undefined'
You have a few typos in your code. For each of your handleChange functions, you should have event.target.value for the state value being set instead of the names of the fields.
Also in your alert function for the name, the value should be this.state.name instead of this.state.value.
Here is a complete working version:
class Profile extends React.Component {
constructor(props) {
super(props);
this.state = { name: "", age: "" };
this.handleNameChange = this.handleNameChange.bind(this);
this.handleAgeChange = this.handleAgeChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleNameChange(event) {
this.setState({ name: event.target.value });
}
handleAgeChange(event) {
this.setState({ age: event.target.value });
}
handleSubmit(event) {
alert("Name was submitted: " + this.state.name);
alert("Age was submitted: " + this.state.age);
event.preventDefault();
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Name:
<input
type="text"
value={this.state.name}
onChange={this.handleNameChange}
/>
</label>
<label>
Age:
<input
type="text"
value={this.state.age}
onChange={this.handleAgeChange}
/>
</label>
<input type="submit" value="Submit" />
</form>
);
}
}
ReactDOM.render(
<Profile / > ,
document.body
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>

Two dropdowns in a React Form

I want to get the users input of a form with two drop-downs, and store it inside a global variable using react. I looked at reacts docs on how to create forms, and manipulate their code a little bit to have two drop-downs, but can't get it to save the variable as a global variable and print that global variable onto the screen. Unfortunately, there was an error when I clicked the second submit button (The first button did nothing). Here was the error: TypeError: this is undefined handleSubmit src/App.js:55 52 | } 53 | handleSubmit(event) { 54 | event.preventDefault(); > 55 | two = this.state.value | ^ 56 | } 57 | 58 | render() { – . Here was my code inside App.js:
import React from "react";
import "./App.css";
var one = "";
var two = "";
class FlavorFormOne extends React.Component {
constructor(props) {
super(props);
this.state = { value: "coconut" };
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({ value: event.target.value });
}
handleSubmit(event) {
event.preventDefault();
one = this.state.value
}
render() {
return (
<div>
<form onSubmit={this.handleSubmit}>
<label>
Pick your favorite flavor:
<select value={this.state.value} onChange={this.handleChange}>
<option value="grapefruit">Grapefruit</option>
<option value="lime">Lime</option>
<option value="coconut">Coconut</option>
<option value="mango">Mango</option>
</select>
</label>
<input type="submit" value="Submit" />
</form>
</div>
);
}
}
class FlavorFormTwo extends React.Component {
constructor(props) {
super(props);
this.state = { value: "GrabeFruit" };
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
this.setState({ value: event.target.value });
}
handleSubmit(event) {
event.preventDefault();
two = this.state.value
}
render() {
return (
<div>
<form onSubmit={this.handleSubmit}>
<label>
Pick your favorite flavor:
<select value={this.state.value} onChange={this.handleChange}>
<option value="grapefruit">Grapefruit</option>
<option value="lime">Lime</option>
<option value="coconut">Coconut</option>
<option value="mango">Mango</option>
</select>
</label>
<input type="submit" value="submit"/>
</form>
</div>
);
}
}
function App() {
return (
<>
<FlavorFormOne />
<FlavorFormTwo />
{one}
{two}
</>
);
}
export default App;
you didn't pass the event
try onSubmit={(e)=>this.handleSubmit(e)}
also onChange={(e)=>this.handleChange(e)}
There were a couple things that needed to be fixed in your code. The first was this.handleSubmit = this.handleSubmit.bind(this); needed to be added in FlavorFormTwo's constructor, as I mentioned in the comments. The second was your handling of global variables. React won't re-render a component when a global variable changes, but it will re-render when the state is changed with setState. This is the reason why react state doesn't update when using this.state =. Instead, I added onSubmit as a prop to both, and inside both handleSubmit functions I added this.props.onSubmit(this.state.value). I changed the App component to a class, and added functions for handleOneSubmit and handleTwoSubmit that set the state of app. Try it online: https://codesandbox.io/s/vibrant-smoke-tsgri?file=/src/App.js
import React from "react";
import "./App.css";
class FlavorFormOne extends React.Component {
constructor(props) {
super(props);
this.state = { value: "coconut" };
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({ value: event.target.value });
}
handleSubmit(event) {
event.preventDefault();
this.props.onSubmit(this.state.value);
}
render() {
return (
<div>
<form onSubmit={this.handleSubmit}>
<label>
Pick your favorite flavor:
<select value={this.state.value} onChange={this.handleChange}>
<option value="grapefruit">Grapefruit</option>
<option value="lime">Lime</option>
<option value="coconut">Coconut</option>
<option value="mango">Mango</option>
</select>
</label>
<input type="submit" value="Submit" />
</form>
</div>
);
}
}
class FlavorFormTwo extends React.Component {
constructor(props) {
super(props);
this.state = { value: "GrabeFruit" };
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({ value: event.target.value });
}
handleSubmit(event) {
event.preventDefault();
this.props.onSubmit(this.state.value);
}
render() {
return (
<div>
<form onSubmit={this.handleSubmit}>
<label>
Pick your favorite flavor:
<select value={this.state.value} onChange={this.handleChange}>
<option value="grapefruit">Grapefruit</option>
<option value="lime">Lime</option>
<option value="coconut">Coconut</option>
<option value="mango">Mango</option>
</select>
</label>
<input type="submit" value="submit" />
</form>
</div>
);
}
}
class App extends React.Component {
constructor(props) {
super(props);
this.state = { one: "", two: "" };
this.handleOneSubmit = this.handleOneSubmit.bind(this);
this.handleTwoSubmit = this.handleTwoSubmit.bind(this);
}
handleOneSubmit(value) {
this.setState({ one: value });
}
handleTwoSubmit(value) {
this.setState({ two: value });
}
render() {
return (
<>
<FlavorFormOne onSubmit={this.handleOneSubmit} />
<FlavorFormTwo onSubmit={this.handleTwoSubmit} />
{this.state.one}
{this.state.two}
</>
);
}
}
export default App;

Prooblem on select in react

I want to create a select component of Blood group option. I write code and it does render on dom but it is not showing up in the browser.
This is FindDonor Component
import React, { Component } from "react";
class FindDonor extends Component {
state = {
value: "Find Donor By Blood Group"
};
handleChange = e => {
this.setState({ value: e.target.value });
};
render() {
return (
<div className="findDonor">
<h1>Hello</h1>
<div className="input-field">
<select value={this.state.value} onChange={this.handleChange}>
<option value="a+">A+</option>
<option value="o+">O+</option>
<option value="b+">B+</option>
<option value="ab+">AB+</option>
<option value="a-">A-</option>
<option value="o-">O-</option>
<option value="b-">B-</option>
<option value="ab-">AB-</option>
</select>
</div>
</div>
);
}
}
export default FindDonor;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
Home Component code
Home.js
App Component code
App.js
You are probably missing a this.handleChange = this.handleChange.bind(this); in your constructor.
Check this example from the React docs (https://reactjs.org/docs/forms.html#the-select-tag):
class FlavorForm extends React.Component {
constructor(props) {
super(props);
this.state = {value: 'coconut'};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({value: event.target.value});
}
handleSubmit(event) {
alert('Your favorite flavor is: ' + this.state.value);
event.preventDefault();
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Pick your favorite flavor:
<select value={this.state.value} onChange={this.handleChange}>
<option value="grapefruit">Grapefruit</option>
<option value="lime">Lime</option>
<option value="coconut">Coconut</option>
<option value="mango">Mango</option>
</select>
</label>
<input type="submit" value="Submit" />
</form>
);
}
}
I think you need to pass event on your onChange onChange={(e) => {this.handleChange(e)}. Also, in case of arrow functions, there is no need to bind functions.
class FlavorForm extends React.Component {
constructor(props) {
super(props);
this.state = {value: 'coconut'};
}
handleChange = (event) => {
this.setState({value: event.target.value});
}
handleSubmit = (event) => {
alert('Your favorite flavor is: ' + this.state.value);
event.preventDefault();
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Pick your favorite flavor:
<select value={this.state.value} onChange={(e) => {this.handleChange(e)}}>
<option value="grapefruit">Grapefruit</option>
<option value="lime">Lime</option>
<option value="coconut">Coconut</option>
<option value="mango">Mango</option>
</select>
</label>
<input type="submit" value="Submit" />
</form>
);
}
}

Adding onBlur Functionality in Forms in Reactjs

I have a simple form build in react js. It has a text area with a submit button. Now I want to add an OnBlur functionality in this form. I tried this in the render method:
render() {
return (
<form onSubmit={this.handleSubmit}> onBlur={this.onBlur}
<label>
Name:
<input type="text" value={this.state.value} onChange={this.handleChange} />
</label>
<input type="submit" value="Submit" />
</form>
);
}
But this is not working. The original code( from FaceBook Tutorials) is as follows: Please note that I know that I need to bind the onBlur() through these lines: this.onBlur = this.onBlur.bind(this);. So that is not an area of Concern.
class NameForm extends React.Component {
constructor(props) {
super(props);
this.state = {value: ''};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({value: event.target.value});
}
handleSubmit(event) {
alert('A name was submitted: ' + this.state.value);
event.preventDefault();
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Name:
<input type="text" value={this.state.value} onChange={this.handleChange} />
</label>
<input type="submit" value="Submit" />
</form>
);
}
}

Resources