我在我的React / Rails项目中使用Semantic-UI-React并尝试使用Form.Select较大菜单中的下拉菜单。我需要从显示用户.name(animal.gnames)的选择中获取.id(animal.id)值-gnmaes只是我出于自己的目的而使用的属性,以避免混淆。
我是Form.Select的options=
道具,我映射了一组项目,然后将item.gname分配给title
,以获得用户的不错的下拉列表。然后,我需要获取此大数组中的id并将其发送到提取请求。我已经尝试过道具的每种组合,但是无法从数组中获取ID并同时显示名称。
这是代码。我切掉了后半部分,因为它与问题无关:
import React, { Component } from "react";
import { withRouter } from "react-router-dom"
import ActiveStorageProvider from 'react-activestorage-provider'
import { Menu, Button, Modal, Header, Icon, Image, Form, Input, Select, Label } from "semantic-ui-react"
import { Link } from "react-router-dom"
const value = null
const options = null
class Navigation extends Component {
state = { modalOpen: false,
title: "",
body: "",
animal: "",
geotag: false,
animals: []
};
componentDidMount() {
fetch('http://localhost:9000/api/v1/animals')
.then(r => r.json())
.then(animals => {
this.setState({ animals: animals })
})
};
handlechange = (event) => {
this.setState({ [event.target.name]: event.target.value })
};
submitForm = (event) => {
this.props.postSighting(this.state.title, this.state.body)
// NEED TO GET ANIMAL.ID IN HERE!!!
};
// THIS METHOD IS READY TO GO FOR DOING THE ITERATION AND SET STATE BUT COULD NOT GET IT OT WORK.
// HAD ISSUE WITH A FETCH AND SET STATE LOOP CALLS
selectAnimal = () => {
const mappedAnimals = this.state.animals.map(animal => {
this.setState({ animal: animal.gname })
})
return mappedAnimals
};
render () {
return (
//.......SKIP TO THE CODE THAT MATTERS
<Form.Select
label="Animal"
name="Animal"
onChange={this.handlechange}
selection= // WHAT GOES HERE?
width={8}
value= // WHAT GOES HERE?
compact
options={this.state.animals.map(animal => ({
return
name: animal.id,
key: animal.id,
value: animal.id,
text: animal.gname
}))}
/> // HOW DO I GET ANIMAL.ID TO MY SUBMIT FORM FUNCTION?
答案 0 :(得分:0)
这里发生了很多事情,所以让我尝试在代码注释中逐一细分。让我知道是否不清楚。
class Navigation extends Component {
state = {
modalOpen: false,
title: "",
body: "",
animal: "",
geotag: false,
animals: []
};
componentDidMount() {
fetch("http://localhost:9000/api/v1/animals")
.then(r => r.json())
.then(animals => {
this.setState({ animals: animals });
});
}
/**
* When a user selects an option in the dropdown, the following will happen:
* 1. this.handlechange gets called, where event.target.name === 'Animal'
* and event.target.value is the id of the animal selected
* 2. Component's state gets updated to look like:
* {
* ...state,
* "Animal": <id of animal selected>
* }
* Side note: you will have to access this "Animal" value through this.state.Animal
* where the uppercase 'A' is very important. This is really prone to typos and not
* recommended. To fix this I would change your <Form.Select> to have a lowercase
* name attribute.
*/
handlechange = (event, { name, value }) => {
// We need to use { name, value } instead of event.target.name, etc
// due to how Semantic UI handles events. More info here:
// https://github.com/Semantic-Org/Semantic-UI-React/issues/638
this.setState({ [name]: value });
};
submitForm = event => {
this.props.postSighting(this.state.title, this.state.body);
// Recall from above that this.state.Animal contains the animal id currently selected
// so we can access it directly with this.state.Animal
const animalId = this.state.Animal;
// do stuff with animalId...
};
/**
* If this is called, it will destroy the array of animals you have in your state and replace it with a gname
* from one of the animals. You probably don't want this to happen.
* Not sure what you are trying to do here, what do you use the return value for?
* Also, this will return an array filled with 'undefined' since you don't return anything in the animals.map callback.
*/
selectAnimal = () => {
const mappedAnimals = this.state.animals.map(animal => {
this.setState({ animal: animal.gname });
});
return mappedAnimals;
};
render() {
return (
//.......SKIP TO THE CODE THAT MATTERS
<Form.Select
label="Animal"
name="Animal" // this is the name that gets passed to 'handlechange' as event.target.name
onChange={this.handlechange}
// selection: this isn't a valid prop for Form.Select and will be ignored regardless of value
width={8}
// value: we don't need to worry about this since Form.Select will handle it
compact
options={this.state.animals.map(animal => ({
// return: was this a typo?
name: animal.id,
key: animal.id,
value: animal.id, // This is the value that gets passed to 'handlechange' as event.target.value
text: animal.gname
}))}
/>
);
}
}
答案 1 :(得分:0)
经过大量研究和@miyu的一些帮助,事实证明您可以这样做:
<Form.Select
fluid
onChange={this.handleChange}
placeholder='select animal'
label="Animal"
name="animal"
width={8}
selection
options={this.state.animals.map(animal => ({
name: animal.gname,
key: animal.id,
value: animal.id,
text: animal.gname
}))}
/>
和您的handleChange
应该如下所示:
handleChange = (e, { name, value }) => this.setState({ [name]: value })