亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

類型錯誤:無法讀取未定義的屬性“值” - React-typeahead

類型錯誤:無法讀取未定義的屬性“值” - React-typeahead

蝴蝶刀刀 2023-07-20 15:46:22
我正在使用react-bootstrap-typeahead組件來預測用戶在文本框中的輸入,但我在設置狀態時遇到問題,以便用戶可以單擊下拉菜單中的選項。到目前為止我的代碼就在這里。該函數handleAddtask在輸入時將任務添加到任務列表,但我無法將其分配給預先輸入下拉列表。(我仍在學習反應,因此任何有關這方面的資源也將不勝感激)。import React, { Component } from "react";import PropTypes from "prop-types";import { Typeahead } from "react-bootstrap-typeahead";class AddWatchlistForm extends Component {? constructor(props) {? ? super(props);? ? this.state = {? ? ? taskName: ""? ? };? ? this.handleAddTask = this.handleAddTask.bind(this);? }? static propTypes = {? ? newTask: PropTypes.func.isRequired? };? render() {? ? return (? ? ? <div className="row">? ? ? ? <div className="col-md-6 col-md-offset-3">? ? ? ? ? <div style={{ margin: "20px" }}>? ? ? ? ? ? <div className="row">? ? ? ? ? ? ? <div className="col-md-6">? ? ? ? ? ? ? ? <Typeahead? ? ? ? ? ? ? ? ? placeholder=""? ? ? ? ? ? ? ? ? onChange={e => this.updateTaskName(e)}? ? ? ? ? ? ? ? ? onClick={(e => this.updateTask(e), this.handleAddTask)}? ? ? ? ? ? ? ? ? value={this.state.taskName}? ? ? ? ? ? ? ? ? onKeyPress={e => this.checkEnterKey(e)}? ? ? ? ? ? ? ? ? labelKey={option =>? ? ? ? ? ? ? ? ? ? `${option.ticker} ${option.security_type}`? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? options={[? ? ? ? ? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ? ? "": 0,? ? ? ? ? ? ? ? ? ? ? ticker: "A",? ? ? ? ? ? ? ? ? ? ? security_type: "Stock"? ? ? ? ? ? ? ? ? ? },? ? ? ? ? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ? ? "": 1,? ? ? ? ? ? ? ? ? ? ? ticker: "AA",? ? ? ? ? ? ? ? ? ? ? security_type: "Stock"? ? ? ? ? ? ? ? ? ? },? ? ? ? ? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ? ? "": 2,? ? ? ? ? ? ? ? ? ? ? ticker: "AAA",? ? ? ? ? ? ? ? ? ? ? security_type: "Stock"? ? ? ? ? ? ? ? ? ? },? ? ? ? ? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ? ? "": 3,? ? ? ? ? ? ? ? ? ? ? ticker: "AAAU",? ? ? ? ? ? ? ? ? ? ? security_type: "Stock"? ? ? ? ? ? ? ? ? ? },? ? ? ? ? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ? ? "": 4,? ? ? ? ? ? ? ? ? ? ? ticker: "AACG",? ? ? ? ? ? ? ? ? ? ? security_type: "Stock"? ? ? ? ? ? ? ? ? ? }
查看完整描述

1 回答

?
函數式編程

TA貢獻1807條經驗 獲得超9個贊

建議,使用箭頭函數保留上下文的更好的方法this:


刪除以下內容...


  constructor(props) {

    super(props);

    this.state = {

      taskName: ""

    };

    this.handleAddTask = this.handleAddTask.bind(this);

  }


  handleAddTask(e) {

    let name = e.target.value;

    if (this.state.taskName.trim() !== "")

      this.props.newTask(this.state.taskName);

  }

并添加以下內容:


  state = {

    taskName: ""

  };


  handleAddTask = e => {

    let name = e.target.value;

    if (this.state.taskName.trim() !== "")

      this.props.newTask(this.state.taskName);

  }

TypeError你得到應得的原因undefined也是同樣的原因。我只是嘗試將所有普通函數替換為箭頭函數,現在它們都工作正常。否則,您應該綁定到this每個類屬性函數,這是一個繁瑣的雙重過程并且性能密集。


另外,在handleAddTask功能中,請更新:


  handleAddTask = e => {

    let name = e.target.value;

    if (this.state.taskName.trim() !== "")

      this.props.newTask(name);

  };

解決方案


問題在于e傳遞給的 。將您的updateTaskName功能更改為:


  updateTaskName = e => {

    this.setState({ taskName: e.length > 0 ? e[0].security_type : "" });

  };

這是因為,e是:


e = {

  "": 2,

  ticker: "AAA",

  security_type: "Stock"

};

工作演示: https://codesandbox.io/s/confident-moore-wsq5p


查看完整回答
反對 回復 2023-07-20
  • 1 回答
  • 0 關注
  • 176 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號