如何正确编写可以接收道具的React类组件?

约书亚(Joshua Rajandiran)

我知道如何在演示组件中接收道具,但目前我还需要使用具有逻辑的函数,因此现在需要将我的组件更改为类组件,我不知道为什么我无法接收该组件道具。

这是我组件的一部分:

class MemberInfoSubPage extends React.Component {
  constructor(props) {
    super(props);
    this.state = {

    };
   this.renderRow = this.renderRow.bind(this);
  }

如您所见,我正在使用ES6,并且正在尝试从地图上渲染行,但现在我只是在尝试接收道具。此代码是否正确?我的意思是通常的语法。

PS:有关其他信息,我没有定义“ props”。所以,是的,更改组件后我没有收到道具。以前我能够收到道具。

编辑

import React, {PropTypes} from 'react';
import ons from 'onsenui';
import * as Ons from 'react-onsenui';

class MemberInfoSubPage extends React.Component {
  //const result = FlightApi.getAllFlightList();
  constructor(props) {
    super(props);
    this.state = {

    };
   // this.stateToEntry = this.stateToEntry.bind(this);
   this.renderRow = this.renderRow.bind(this);
  }

  renderRow(row,index) {
    const x = 40 + Math.round(5 * (Math.random() - 0.5)),
          y = 40 + Math.round(5 * (Math.random() - 0.5));

    const names = ['Max', 'Chloe', 'Bella', 'Oliver', 'Tiger', 'Lucy', 'Shadow', 'Angel'];
    const name = names[Math.floor(names.length * Math.random())];

    return (
      <Ons.ListItem key={index}>
        <div className='left'>
          <img src={`http://placekitten.com/g/${x}/${y}`} className='list__item__thumbnail' />
        </div>
        <div className='center'>
          {name}
        </div>
      </Ons.ListItem>
    );
  }

  render() {
      if (props['index'] == 0) {
        return (
          <div className="memberInfoSubPage">
            <div className="memberInfoSubPage-row1">

              <span>{props['data-user'].id}</span>

              <table border={1} className="memberInfoSubPage-Table">
                <tr>
                  <th style={{color: 'grey'}}>Rank</th>
                  <th style={{color: 'grey'}}>Country</th>
                </tr>
                <tr>
                  <td>{props['data-user'].rank}</td>
                  <td>{props['data-user'].country}</td>
                </tr>
              </table>
            </div>
            <div>
              <div className="memberInfoSubPage2-Summary-Title">Placement Performance Summary</div>
              <table border={1} className="memberInfoSubPage-Table2">
                <tr>
                  <td>L</td>
                  <td>R</td>
                </tr>
                <tr>
                  <td>{props['data-user'].placementPerformanceSummary.L}</td>
                  <td>{props['data-user'].placementPerformanceSummary.R}</td>
                </tr>
              </table>
            </div>
             <div>
              <div className="memberInfoSubPage2-Summary-Title">Today Detail</div>
              <table border={1} className="memberInfoSubPage-Table3">
                <tr>
                  <td>L</td>
                  <td>R</td>
                </tr>
                <tr>
                  <td>{props['data-user'].todayDetail.L}</td>
                  <td>{props['data-user'].todayDetail.R}</td>
                </tr>
              </table>
            </div>
            <div> <table border={1} className="memberInfoSubPage-Table3">
                <tr><th style={{color: 'grey'}}>Next Level Upgrade</th></tr>
                <tr>
                  <td>{props['data-user'].nextLevelUpgrade}</td>
                </tr>
              </table>
            </div>

            <Ons.Button style={{margin: '6px'}}>Instant Upgrade</Ons.Button>


            <div>
              <div className="memberInfoSubPage2-Summary-Title" style={{color: 'grey'}}>Conversion Share Platform Portfolio</div>
              <table border={1} className="memberInfoSubPage-Table3">
                <tr style={{color: 'grey'}}>
                  <th>Market($)</th>
                  <th>Unit</th>
                  <th>Tradable Unit</th>
                </tr>
                <tr>
                  <td>{props['data-user'].market}</td>
                  <td>{props['data-user'].unit}</td>
                  <td>{props['data-user'].tradableUnit}</td>
                </tr>
              </table>
            </div>
             <div><table border={1} className="memberInfoSubPage-Table3">
                <tr style={{color: 'grey'}}>
                  <th>Lock Units</th>
                  <th>Avg Price</th>
                  <th>Last Price</th>
                </tr>
                <tr>
                  <td>{props['data-user'].lockUnits}</td>
                  <td>{props['data-user'].avgPrice}</td>
                  <td>{props['data-user'].lastPrice}</td>
                </tr>
              </table>
            </div>
          </div>
        );
      }
      else  if (props['index'] == 1) {
        return (
            <Ons.List
            dataSource={[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]}
            renderRow={this.renderRow}
            renderHeader={() => <Ons.ListHeader>Summary</Ons.ListHeader>}/>
              /*<div className="memberInfoSubPage2-Summary-Title">Summary</div>
              <table className="memberInfoSubPage2-Summary-Table">
                <tr><td>Credit</td><td>{props['data-user'].summary.credit}</td></tr>
                <tr><td>Register</td><td>{props['data-user'].summary.register}</td></tr>
                <tr><td>CP(S)</td><td>{props['data-user'].summary.cpS}</td></tr>
                <tr><td>CP(0)</td><td>{props['data-user'].summary.cp0}</td></tr>
                <tr><td>AP</td><td>{props['data-user'].summary.ap}</td></tr>
                <tr><td>BO Point</td><td>{props['data-user'].summary.boPoint}</td></tr>
                <tr><td>Listed Company Fund</td><td>{props['data-user'].summary.listedCompanyFund}</td></tr>
                <tr><td>Promo</td><td>{props['data-user'].summary.promo}</td></tr>
                <tr><td>TT</td><td>{props['data-user'].summary.tt}</td></tr>
                <tr><td>Re-Entry Point</td><td>{props['data-user'].summary.reEntryPoint}</td></tr>
              </table>*/
        );
      }
      else {
        return (
          <p>Not receiving any index. No content can be shown.</p>
        );
      }
  }
};

MemberInfoSubPage.propTypes = {
  'data-pageName': PropTypes.string.isRequired,
  name: PropTypes.string.isRequired,
  onChange: PropTypes.func.isRequired,
  'defaultOption': PropTypes.string,
  value: PropTypes.string,
  'error': PropTypes.string,
  'options': PropTypes.arrayOf(PropTypes.object)
};

export default MemberInfoSubPage;

这是我的代码,我敢肯定我错过了一些东西。仍然有许多未改进的代码,并且该函数renderRow和该Onsen列表已复制粘贴。

中学

props在组件实例上,因此您需要引用它,this.props而不仅仅是props在您的render函数中。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

嵌套的React / Relay组件未接收道具

来自分类Dev

React 组件接收到错误的道具值

来自分类Dev

如何编写脚本任务来测试将键盘状态作为道具传递的 React 组件

来自分类Dev

如何在Typescript中正确键入React ErrorBoundary类组件?

来自分类Dev

如何正确访问组件内部的道具

来自分类Dev

如何在类组件中定位道具

来自分类Dev

React可重用按钮组件-如何正确传递背景颜色道具?

来自分类Dev

如何使用Link(react-router)将道具从一页传递到类组件

来自分类Dev

如何将其编写为类组件?

来自分类Dev

如何将道具从功能组件传递到类组件

来自分类Dev

如何将ref与React类组件和样式化组件一起正确使用?

来自分类Dev

如何使用道具导出React组件?

来自分类Dev

如何根据条件选择React组件道具

来自分类Dev

React-如何等待组件道具

来自分类Dev

如何在React中编写Jest测试以检查组件是否具有CSS类?

来自分类Dev

React Native - 将类作为道具传递给组件

来自分类Dev

嵌套的React组件可以有不同的道具吗?

来自分类Dev

嵌套的React组件可以有不同的道具吗?

来自分类Dev

访问React组件的道具

来自分类Dev

如何使用react正确编写条件类型?

来自分类Dev

如何使用JSDocs注释ReactJS状态完整的“类”组件道具

来自分类Dev

使用react-transition-group作为子组件接收新道具

来自分类Dev

在React类中应如何编写函数?

来自分类Dev

如何构造一个从redux接收道具的组件,进行查询并将结果保存回redux?

来自分类Dev

如何正确设置对React组件的引用?

来自分类Dev

如何在React组件上测试道具更新

来自分类常见问题

React和Typescript:如何扩展通用组件道具?

来自分类Dev

如果道具为空,如何防止React组件安装?

来自分类Dev

使用React Router,如何使用路由配置传递组件道具?

Related 相关文章

  1. 1

    嵌套的React / Relay组件未接收道具

  2. 2

    React 组件接收到错误的道具值

  3. 3

    如何编写脚本任务来测试将键盘状态作为道具传递的 React 组件

  4. 4

    如何在Typescript中正确键入React ErrorBoundary类组件?

  5. 5

    如何正确访问组件内部的道具

  6. 6

    如何在类组件中定位道具

  7. 7

    React可重用按钮组件-如何正确传递背景颜色道具?

  8. 8

    如何使用Link(react-router)将道具从一页传递到类组件

  9. 9

    如何将其编写为类组件?

  10. 10

    如何将道具从功能组件传递到类组件

  11. 11

    如何将ref与React类组件和样式化组件一起正确使用?

  12. 12

    如何使用道具导出React组件?

  13. 13

    如何根据条件选择React组件道具

  14. 14

    React-如何等待组件道具

  15. 15

    如何在React中编写Jest测试以检查组件是否具有CSS类?

  16. 16

    React Native - 将类作为道具传递给组件

  17. 17

    嵌套的React组件可以有不同的道具吗?

  18. 18

    嵌套的React组件可以有不同的道具吗?

  19. 19

    访问React组件的道具

  20. 20

    如何使用react正确编写条件类型?

  21. 21

    如何使用JSDocs注释ReactJS状态完整的“类”组件道具

  22. 22

    使用react-transition-group作为子组件接收新道具

  23. 23

    在React类中应如何编写函数?

  24. 24

    如何构造一个从redux接收道具的组件,进行查询并将结果保存回redux?

  25. 25

    如何正确设置对React组件的引用?

  26. 26

    如何在React组件上测试道具更新

  27. 27

    React和Typescript:如何扩展通用组件道具?

  28. 28

    如果道具为空,如何防止React组件安装?

  29. 29

    使用React Router,如何使用路由配置传递组件道具?

热门标签

归档