javascript - 如何在 React 中映射数组并将其作为 props 正确传递给子组件?

标签 javascript reactjs react-props react-component

我有一个子组件PatientRow,它从其父组件ObsTabel接收 Prop 中映射的患者数组。奇怪的是 PatientRow 从未被渲染,而且我在 React Dev 工具上也找不到 PatientRow 组件。另一方面,如果我只是传递患者数组,然后将其映射到 PatienRow 中,组件就会被渲染,但这不是我想要的。我想映射 ObsTabel 中的数组并将其传递给 PatientRow ,以便行是具有自己状态的单独组件。如果您能发现我的错误,请告诉我。 注意:ObsTable 有一个父组件,它从那里接收作为 props 传递的患者数组。文件结构 - ParentComponent(带有患者数组).js>ObsTable.js>PatientRow.js


This doesn’t exist in DOM 

export default class PatientRow extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            disabled: false
        };

    }

    render() {

        const { id, label, name, room, timestamp, observationLevel, roomStatus, patient, index } = this.props;
        console.log(this.props);

        return (
            <tbody>

                <tr key={id} >
                    <td>{label}</td>
                    <td>{name}</td>
                    <td>{observationLevel}</td>
                    <td>{roomStatus}</td>
                    <td>{timestamp} </td>

                    <td>


                        <div>
                            <Button> Awake </Button>
                            <Button>Asleep/Breathing</Button>
                        </div>

                        <Button> View Room </Button>

                        <div>
                            <Button>Awake </Button>
                        </div>

                    </td>
                    <td>
                        <Button>Asleep</Button>
                    </td>
                </tr>
            </tbody>
        );
    }
}

export default class ObsTabel extends React.Component {
    constructor(props) {
        super(props);

    }

    render() {


        return (
            <div>
                <div >
                    <Table bordered striped hover >
                        <thead>
                            <tr>
                                <th>Room</th>
                                <th>Patient Name</th>
                                <th> Obs Level </th>
                                <th>Room Status</th>
                                <th>Obs Due In</th>
                                <th>Patient Location</th>
                                <th>Patient Obs Detail</th>
                                <th>Comment</th>
                            </tr>
                        </thead>

                        {this.props.patients.map((patient, index) => {
                            // console.log(patient); logs each patient 

                            <div key={index}>
                                <PatientRow
                                    name={patient.name}
                                    id={patient.id}
                                    label={patient.label}
                                    room={patient.room}                                            observationLevel={patient.observationLevel}
                                    roomStatus={patient.roomStatus}
                                    timestamp={patient.timestamp}
                                    index={index}           
                                />
                            </div>;
                        })}

                    </Table>
                </div>



            </div>
        );
    }
}

最佳答案

这是因为您没有在 map 内返回任何内容

return (
   <div key={index}>
     <PatientRow
       name={patient.name}
       id={patient.id}
       label={patient.label}
       room={patient.room}                                            
       observationLevel={patient.observationLevel}
       roomStatus={patient.roomStatus}
       timestamp={patient.timestamp}
       index={index}           
     />
  </div>
 );

关于javascript - 如何在 React 中映射数组并将其作为 props 正确传递给子组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57954217/

相关文章:

javascript - 我可以在 RegExp 声明中即时执行内联函数吗?

Javascript自动大写输入字段中每个单词的首字母

django - 使用 Webpack 配置 React 路由、Django URL

javascript - 在 React 中将数据从子级传递给父级

html - 如何对齐面板右下角的按钮

reactjs - react 警告 : Failed prop type: Invalid prop of type `Object` supplied

javascript - React Child prop 变量未定义,即使我之前定义了该变量

javascript - 我如何才能确信用户没有更改文本框值?

javascript - JSX 与组件类实例化

typescript - 我怎样才能让 TypeScript IntelliSense 在受歧视的联合中建议有效的完成?