How can I use the options in the select in my state?
this.state = {
posts: [],
question: '',
body: '',
postType: '',
}
<select value="types" className="u-full-width">
<option value="Option 1">Question</option>
<option value="Option 2">Discussion</option>
<option value="Option 3">Clout</option>
</select>
I am not sure how to set the state of postType based off what the user selects.
getPostType() {
const type =
this.setState({ postType: type });
}
You need to add a function to handle the changes on the select, save the value in the state and pass it to the select.
Something like this:
class App extends React.Component {
constructor(props){
super(props);
this.state = {
postType: 'Option 1',
}
this.onChange = this.onChange.bind(this)
};
onChange(event){
this.setState({
postType: event.target.value
})
}
render() {
return (
<div>
<select value={this.state.postType} onChange={this.onChange} className="u-full-width">
<option value="Option 1">Question</option>
<option value="Option 2">Discussion</option>
<option value="Option 3">Clout</option>
</select>
{this.state.postType}
</div>
);
}
}
ReactDOM.render(
<App />,
document.getElementById('container')
)
<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="container"></div>
This is called a controlled component. You can read more about it here.
I think that what you're looking for is the following:
<select className="u-full-width"
onChange= this.handleOnChange.bind(this)
ref={value => this.refName = value}>
<option value="Option 1">Question</option>
<option value="Option 2">Discussion</option>
<option value="Option 3">Clout</option>
</select>
handleOnchange(e){
e.preventDefault(e);
const type = this.refName.value;
setState({postType: type});
}
Hope it was helpful!
You can try the following implementation.
class Sample extends React.Component {
constructor(props) {
super(props);
this.state = {
posts: [],
question: '',
body: '',
postType: 'Question',
}
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
this.setState({postType: event.target.value});
}
render() {
return (
<form>
<label>
Select your postType:
<select className="u-full-width" value={this.state.postType}
onChange={this.handleChange}>
<option value="Question">Question</option>
<option value="Discussion">Discussion</option>
<option value="Clout">Clout</option>
</select>
</label>
</form>
);
}
}
ReactDOM.render(
<Sample />,
document.getElementById('root')
);
Related
I'm a beginner with react and I want use it for sand a form and change the page. This is my code:
class Home extends React.Component {
constructor(props) {
super(props);
this.state = {type: true, number: true};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
const target = event.target;
const value = target.type === 'type' ? target.checked : target.value;
const name = target.name;
this.setState({
[name]: value
});
}
async handleSubmit(event) {
event.preventDefault();
try{
await axios.post("http://localhost:5000/number",{
type: this.state.type,
number: this.state.number
});
}catch(error){
console.log(error)
}
}
render() {
return (
<div className="tabSearch">
<form onSubmit={this.handleSubmit}>
<select className="input" value={this.state.value} name="type" onChange={this.handleChange}>
<option className="first_select" value="">0</option>.
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select className="input" value={this.state.value} name="number" onChange={this.handleChange}>
<option className="first_select" value="">number</option>.
<option value="1">1</option>
<option value="2">2</option>
<option value="0">0</option>
</select>
<input className="button" type="submit" value="Submit" />
</form>
</div>
);
}
}
export default Home;
I tried to use "Navigate" but it gives me some problems because I use a react.component, so I wanted to know how to solve this problem pls T.T
You can use react router.
Official docs: https://reactrouter.com/en/main
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>
);
}
}
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>
);
}
}
hi guys I'm trying to reset a select tag after the submit using React,I connected the first option to the state which is :
state = {
inputs: [],
tempInput: {
inputType: 'Please select a type'
}
};
so I basically select a type in my form, it updates the tempInput object with the inputType, and then add it to the array of objects,
<div className="formG">
<form className="form-maker" onSubmit={this.handleSubmit}>
<select onChange={this.onSelect}>
<option>{this.state.tempInput.inputType}</option>
<option value="text">text</option>
<option value="color">color</option>
<option value="date">date</option>
<option value="email">email</option>
<option value="tel">tel</option>
<option value="number">number</option>
</select>
<button>Submit</button>
</form>
this is my on select method:
onSelect = ({ target }) => {
const { tempInput } = this.state;
tempInput.inputType = target.value;
this.setState({ tempInput });
};
handleSubmit = e => {
e.preventDefault();
how to do that in handleSubmit? to put the tempInput.inputType to ="Please pick a type"
};
This is an uncontrolled element.
If you want to control the value of an input / select you need to set it via your state:
const values = [
"text", "color", "date", "email", "tel","number"
]
class App extends React.Component {
state = { value: "" };
onSelect = ({target}) => this.setState({value: target.value})
handleSubmit = () => {
console.log('submit with ',this.state.value)
this.setState({value: ''})
}
render() {
const { value } = this.state;
return (
<div>
<select onChange={this.onSelect}>
<option>Select A value</option>
{values.map(val => <option key={val} value={val} selected={val === value}>{val}</option>)}
</select>
<button onClick={this.handleSubmit}>Submit</button>
<div>{`Selectet Value is ${value}`}</div>
</div>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
<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="root" />
Here is the example without the array:
class App extends React.Component {
state = { value: "" };
onSelect = ({ target }) => this.setState({ value: target.value })
handleSubmit = () => {
console.log('submit with ', this.state.value)
this.setState({ value: '' })
}
render() {
const { value } = this.state;
return (
<div>
<select onChange={this.onSelect}>
<option selected={value === ""} value="">Select A value</option>
<option selected={value === "text"} value="text">text</option>
<option selected={value === "color"} value="color">color</option>
<option selected={value === "date"} value="date">date</option>
<option selected={value === "email"} value="email">email</option>
<option selected={value === "tel"} value="tel">tel</option>
<option selected={value === "number"} value="number">number</option>
</select>
<button onClick={this.handleSubmit}>Submit</button>
<div>{`Selectet Value is ${value}`}</div>
</div>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
<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="root"/>
Of course there is a lot of repeated code here, in programming there is the "DRY" principle (Do Not Repeat Yourself).
This is why we use loops like Array.prototype.map
in your onSelect function, you're mutate the state object(tempInput.inputType = target.value;), it is not a good practice in react.
if you want your select value controlled by the react state, first you need to bind it's value with react state, which it's called a controlled component, like:
<select onChange={this.onSelect} value={this.state.tempInput.inputType}>
Hi all I am unable to set the value of value1 if I am changing the value from parent select. I am able to render different select on changing the options of parent select, but I am not able to set the value.
import React, { Component } from 'react';
class FlavorForm extends React.Component {
constructor(props) {
super(props);
this.state = {value: 'HR',value1 : '1'};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.handleChange1 = this.handleChange1.bind(this);
}
handleChange(event) {
this.setState({value: event.target.value});
}
handleSubmit(event) {
event.preventDefault();
alert(this.state.value , this.state.value1);
}
getsecondselect = () => {
if(this.state.value === "HR"){
//gethrcontent = () => {
return (
<label>
Pick your HR:
<select value1={this.state.value1} onChange={this.handleChange1}>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</label>
)//};
}
else{
//getslectedenrollment = () => {
return (
<label>
Pick your Enrollment:
<select value1={this.state.value1} onChange={this.handleChange1}>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
</select>
</label>
)//};
}
}
handleChange1(event) {
this.setState({value1: event.target.value1});
alert(this.state.value1);
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Pick your favorite flavor:
<select value={this.state.value} onChange={this.handleChange}>
<option value="HR">HR</option>
<option value="enrollment">enrollment</option>
</select>
</label>
{getsecondselect()}
<input type="submit" value="Submit" />
</form>
);
}
}
export default FlavorForm;
I made some changes to the code, but as stackoverfloweth mentioned, you had event.target.value1 instead of event.target.value.
Also, you need this.getsecondselect() in the render function of the code you posted.
Working CodeSandbox here.
import React from "react";
import ReactDOM from "react-dom";
class App extends React.Component {
state = {
value: "HR",
number: "1"
};
handleChangeValue = event => {
this.setState({ value: event.target.value });
};
handleSubmit = event => {
event.preventDefault();
alert(`${this.state.value} --> ${this.state.number}`);
};
getSecondSelect = () => {
if (this.state.value === "HR") {
return (
<label>
Pick your HR:
<select value1={this.state.number} onChange={this.handleChangeNumber}>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</label>
); //};
} else {
//getslectedenrollment = () => {
return (
<label>
Pick your Enrollment:
<select value1={this.state.value1} onChange={this.handleChangeNumber}>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
</select>
</label>
); //};
}
};
handleChangeNumber = event => {
this.setState({ number: event.target.value });
};
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Pick your favorite flavor:
<select value={this.state.value} onChange={this.handleChangeValue}>
<option value="HR">HR</option>
<option value="enrollment">enrollment</option>
</select>
</label>
{this.getSecondSelect()}
<input type="submit" value="Submit" />
</form>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
It looks like both of your selects are setup to have value={this.state.value1} and onchange they both call this.handleChange1. Not sure if that's an issue.
I also noticed that within handleChange1 you are updating the state with a prop event.target.value1 but the options do not have value1 defined.. perhaps you meant to use event.target.value?
Hope this helps