简体   繁体   English

将道具传递给子代孩子

[英]Passing props to component children

I'm having trouble with React by being unable to pass my props to child components. 我无法将我的道具传递给子组件,从而在React遇到了麻烦。 I've read the documentation but I'm still confused as to what steps need to be taken. 我已经阅读了文档,但是对于需要采取哪些步骤仍然感到困惑。 This is done using Ruby on Rails with the react_on_rails gem. 这是通过将Ruby on Rails与react_on_rails gem一起使用来完成的。 At this point, Redux is not in use and I'm still learning the ropes with React. 目前,Redux尚未使用,我仍在使用React学习。

I'm sending in a variable from rails using react_component with the following: 我正在使用react_component从Rails发送一个变量,并带有以下内容:

index.html.erb index.html.erb

<%= react_component('Lifts', props: @lifts, prerender: true) %>

Trying to pick it up in React. 试图在React中拿起它。 The data shows up under the Elements section of the Chrome dev tools. 数据显示在Chrome开发者工具的“元素”部分下。 But, I'm assuming that maybe I'm not binding the data properly. 但是,我假设我可能未正确绑定数据。 Nothing seems to show up in LiftsList.jsx. LiftsList.jsx中似乎没有任何显示。 Perhaps the solution is obvious but it's really escaping me right now. 也许解决方案很明显,但是它确实使我逃脱了。

Lifts.jsx Lifts.jsx

import React, { PropTypes } from 'react';
import LiftsList from './LiftsList';
import Lift from './Lift';
export default class Lifts extends React.Component {

  constructor(props, _railsContext) {
    super(props);
    this.state = {
      id: props.id,
      date: props.date,
      liftname: props.liftname,
      weightlifted: props.weightlifted,
      repsformed: props.repsformed,
    }
  }
  render() {
    return (
      <div className="container" style={{display : 'inline-block'}}>
        <ul>
          <LiftsList lifts = {this.state.lifts} />
        </ul>
      </div>
    );
  }
}

LiftsList.jsx LiftsList.jsx

import React, {PropTypes} from 'react';
import Lift from './Lift';
export default class LiftsList extends React.Component {

  render() {
    let lifts = this.state.lifts.map(lift =>
      <Lift key={prop.id} {...lift} />);
    return (
      <div>
        <div>???</div>
      </div>
    );
  }
}

Lift.jsx Lift.jsx

import React, {PropTypes} from 'react';
export default class Lift extends React.Component {

  render() {
    return (
      <ul>
        <li>{this.props.id}</li>
        <li>{this.props.date}</li>
        <li>{this.props.liftname}</li>
        <li>{this.props.ismetric}</li>
        <li>{this.props.weightlifted}</li>
        <li>{this.props.repsformed}</li>
      </ul>
    );
  }
}

In Lifts, your state appears to be unused, and instead of this.state.lifts I think you want to use this.state.props . 在电梯中,你的state似乎是未使用的,而是this.state.lifts我想你想使用this.state.props

You may instead want something like: 您可能想要以下内容:

  constructor(props, _railsContext) {
    super(props);
  }
  render() {
    return (
      <div className="container" style={{display : 'inline-block'}}>
        <ul>
          <LiftsList lifts={this.props.lifts} />
        </ul>
      </div>
    );
  }

And similarly, in LiftsList, you likely want something like: 同样,在LiftsList中,您可能想要类似以下内容:

render() {
  let lifts = this.props.lifts.map(lift =>
    <Lift key={lift.id} {...lift} />);
  return (
    <div>
      <div>???</div>
    </div>
  );
}

Which replaces state with props and gets your id from the lift props代替state并从lift获取您的身份证

See: What is the difference between state and props in React? 请参阅: 状态和React中的prop有什么区别?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM