繁体   English   中英

在下拉菜单中使用带有对象的状态作为选项:REACT

[英]Use a state with objects as options in dropdown menu : REACT

创建具有一些功能方面的虚拟网站。 为了澄清起见,我一周前开始学习React,所以请对您的回答感到满意。 谢谢!

最初在App.js中创建了一个状态,该状态允许我添加和删除客户端。 (例如老旧的待办事项清单)

现在,使用另一个可以“添加课程”的组件,我想要一个下拉选择器,可以从另一个状态中选择一个现有客户端作为我的选项。 到目前为止,我已经创建了所有需要输入的内容,console.log()表明一切正常。 但是,我似乎无法将下拉选项显示为现有客户端,因此可以将课程添加为其他状态。 为了澄清,我有两个状态,客户和课程(与现有客户)。

这是我的App.js

import React, { Component } from 'react'
import Clients from './components/Clients'
import AddClient from './components/AddClient'
import NavBar from './components/NavBar'
import AddLesson from './components/AddLesson'

    class App extends Component {
      state = {
        clients : [
          {id : 1 , name : 'bob', price : 30},
          {id : 2 , name : 'mary', price : 40},
          {id : 3 , name : 'greg', price : 45}
        ],
        lessons : [
          {id: null, name: '', time: null}
        ]
      }

      deleteClient = (id) => {
        let clients = this.state.clients.filter(client => {
          return client.id !== id
        })
        this.setState({
          clients: clients
        })
      }

      addClient = (newClient) => {
        newClient.id = Math.random();
        let clients = [...this.state.clients, newClient];
        clients.sort((a, b) => a.name.localeCompare(b.name))
        this.setState({
            clients: clients,
        })
      }

      addLesson = (newLesson) => {
        newLesson.id = Math.random(); 
        let lessons = [...this.state.lessons, newLesson];
        this.setState({
            lessons: lessons,
        })
        console.log(lessons)
      }

      render() {
        return (
          <div className="App">
            <NavBar/>
            <Clients clients={this.state.clients} deleteClient={this.deleteClient}/>
            <AddClient addClient={this.addClient}/>
            <AddLesson addLesson={this.addLesson}/>
          </div>
        );
      }
    }

    export default App;

这是AddLesson.js

import React, {Component} from 'react'

class AddLesson extends Component {
    state = {
        lessons: [
            {id : null, name: '', time: null}
        ]
    }

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

    handleSubmit = (e) => {
        e.preventDefault();
        this.props.addLesson(this.state);
        e.target.reset();
    }

    render() {
        return (
            <div className="text-center">
                <h2>Add a lesson</h2>
                <form onSubmit={this.handleSubmit}>

                    <select name="lesson_client">
                        <option>{this.props.clients}</option>
                    </select>

                    {/* <label htmlFor="name">Client Name: </label>
                    <input type="text" id="name" onChange={this.handleChange}/><br/>
                    <label htmlFor="time">Lesson Duration (in hours): </label>
                    <input type="text" id="time" onChange={this.handleChange}/><br/>
                    <button className="btn btn-primary">Add Client</button> */}
                </form>
            </div>
        )
    }
}

export default AddLesson

我尝试不重载代码,因此,如果我缺少某些内容,请告诉我,我将对其进行编辑。

您需要遍历客户端并渲染它们。

<select onChange={(e) => this.handleChange(e)} name="lesson_client">
  {this.props.clients.map(client => <option key={client.id} value={client.price}>{client.name}</option>)}
</select>

您可以听handleChange读取所选的值。 (注意:由于select标记上没有id attr,因此我已将e.target.id更改为e.target.name

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

而且我还注意到您没有将clients传递给AddLesson组件。 请通过它以使代码正常工作。

<AddLesson clients={this.state.clients} addLesson={this.addLesson}/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM