【问题标题】:Pass an object value in props to another component将 props 中的对象值传递给另一个组件
【发布时间】:2020-02-25 12:26:18
【问题描述】:

考虑下面的代码,我需要通过 props 将对象格式的 id 传递给另一个组件。但我尝试了很多次,但它不起作用。我想我可能有一些错误,但我不确定它在哪里。

主页(更新):

render(props){
const data = this.state.data;
return (
    <div>
        <Header />
        <NavigationBar />
        <PurchaseInfoView show={this.state.displayModal} closeModal={this.closeModal} value={this.openModal}/>
          <div className="purchase-side">
            <div className="side-title">
              <h1>Purchase Order List</h1>
            </div>
            <hr class="solid" />
            {
              Object.keys(data).map((key) =>
                <div className="list-item">
                  <h2 onClick= {() => this.openModal(data[key].id)}> //get id
                    { data[key].item_name}
                  </h2>
                </div>
            )}
          </div>
          <div className="dads">        
        </div>
        </div>

    );
}

openModal(更新):

  openModal = (id) => {
    this.setState(
      {
        displayModal: true,
        id: id
      });
    console.log(id) // i can get the id from here id=1
  };

PurchaseInfoView 获取 id(更新)。

class PurchaseInfoView extends Component {
 render() {
        console.log(this.props.id) // get undefined
        return (
        <div className="Modal"
            style={{
                transform: this.props.show ,
                opacity: this.props.show ? "1" : "0"
              }}
            >

            <h3>Purchase Order detail</h3>
            <p>Id: {this.props.id}</p> //cannot get it
        </div>
        );
    }
}

export default PurchaseInfoView;

console.log 结果:

【问题讨论】:

  • 不应该是this.props.id而不是PurchaseInfoView中的this.props.value.id吗?
  • 你不会像 props 一样传递 id。Ofc 将是未定义的。

标签: reactjs react-props


【解决方案1】:

如果你想将一个对象传递给 props,步骤如下:

  1. 在您的父母状态中定义对象。
  2. 调用组件时在props中传递对象
  3. 从子项的 props 中获取对象。

这里你错过了第二步!

你应该试试这些:

主页

render(props){
const { data, modalObject, displayModal } = this.state; //use destructuring is more readable
return (
    <div>
        <Header />
        <NavigationBar />
        <PurchaseInfoView show={displayModal} closeModal={this.closeModal} modalObject={modalObject}/> //pass the object from destructuring state as props
        <div className="purchase-side">
          <div className="side-title">
            <h1>Purchase Order List</h1>
          </div>
          <hr class="solid" />
          {
            Object.keys(data).map((key) =>
              <div className="list-item">
                <h2 onClick= {() => this.openModal(data[key].id)}> //get id
                  { data[key].item_name}
                </h2>
              </div>
          )}
        </div>
        <div className="dads">        
      </div>
      </div>
    );
}

OpenModal

  openModal = (id) => {
    this.setState(
      {
        displayModal: true,
        modalObject: {id: id, ...any others key/val pair}
      });
  };

PurchaseInfoView

class PurchaseInfoView extends Component {
 render() {
        const { modalObject} = this.props; //here get your object from props
        console.log(modalObject.id);// here you have the object
        return (
        <div className="Modal"
            style={{
                transform: this.props.show ,
                opacity: this.props.show ? "1" : "0"
              }}
            >

            <h3>Purchase Order detail</h3>
            <p>Id: {modalObject.id}</p>
        </div>
        );
    }
}

如果您有任何问题,请在评论中告诉我;)

注意:如果您在模态中需要更多的东西而不仅仅是 id,我会使用一个对象(又名 {})来执行此操作。如果只需要 id,您只需将 modalObject 替换为您需要的“id”

干杯!

编辑:要使此解决方案起作用,您必须:

  • 至少初始化你的状态:

    this.state={ modalObject : { id: ''}}

  • 或在显示元素之前在您的子组件中进行非空测试,如下所示:

    Id: {modalObject && modalObject.id ? modalObject.id : ' '}

这些是必需的,因为在第一次渲染时,您的状态将具有您设置的初始状态,因此如果您没有设置任何内容或没有测试值......好吧......它是未定义的! :)

(请注意,如果 id 为 null 而不是未定义的错误,您的模态中将显示一个空格)

【讨论】:

  • 嗨@Palencar。感谢您的清晰解释。但是当我尝试用您的更正来更新我的代码时,我收到“TypeError:无法读取未定义的属性 'id'”的错误。
  • 可能与objectModal ={modalObject}有关吗?​​
  • 是的,对不起,我在这里打错字了,我到处都用 modalObject 编辑了代码;),我还为你得到的错误添加了一个编辑,不要忘记初始化你的状态,这样你就没有未定义的。其他解决方案是在子组件中进行非空测试,以查看模态对象是否在之前定义在渲染中添加需要它的元素
  • 很好,感谢您的指导。
【解决方案2】:

猜你叫错了。应该是 {this.props.id}

render() {
        console.log(this.props.id);
        return (
        <div className="Modal">
            <h3>Purchase Order detail</h3>
                <p>Id: {this.props.id}</p>  //Changed line
        </div>
        );
    }



在主页面内部将 id 传递给 PurchaseInfoView 并将其作为道具访问


   <PurchaseInfoView show={this.state.displayModal} closeModal={this.closeModal} value={this.openModal} id={this.state.id}/>


【讨论】:

  • 您好,感谢您的指导。但我仍然在 console.log 中未定义
  • 嗨@sv12,当然。我已经更新了代码。请看一下
  • @kokoka 它没有被发送到 PurchaseInfoView 组件
  • 哦,我明白了。应该是
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-09
  • 2023-03-24
  • 2020-05-03
  • 2018-09-05
  • 2021-12-31
  • 1970-01-01
相关资源
最近更新 更多