将 props 中的对象值传递给另一个组件

Pass an object value in props to another component

考虑下面的代码,我需要通过 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}

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 传递给 Purchase InfoView 并作为道具访问它


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


如果你想将 object 传递给道具,请按以下步骤操作:

  1. 在您的 parents 状态中定义 object。
  2. 调用组件时传入 props 中的object
  3. 从 child 中的道具获得 object。

这里你错过了第二步!

你应该试试这些:

主页

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,我会使用 object(又名 {})来完成此操作。如果只需要 id 你只需要用你需要的 "id" 替换 modalObject

干杯!

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

  • 至少将你的状态初始化为:

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

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

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

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

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