简体   繁体   English

通过prop将状态值传递给子组件

[英]passing state value to a child component via props

i'm trying to pass the value entered by the user from the app component to the passTicket component. 我正在尝试将用户输入的值从应用程序组件传递到passTicket组件。 I tried invoking props to pass this state data but I keep getting an undefined error when attempting to access it. 我尝试调用道具传递此状态数据,但是尝试访问它时始终收到未定义的错误。 I'm new to react and it would be great if someone can help me make sense of what i'm getting wrong. 我是新来的人,如果有人可以帮助我弄清我的错,那将是很棒的。 This is a sample of what i'm trying to achieve. 这是我要实现的示例。 This is my main component: 这是我的主要组成部分:

class App extends Component {

  constructor(props){
    super(props);
    this.state = {
      ticket:"",
    };
    this.changeTicket = this.changeTicket.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
    this.keyPress = this.keyPress.bind(this);
  }

  changeTicket(e){
    this.setState({
      ticket : e.target.value,
    })
  }

  handleSubmit(){
    this.setState({
      updatedTicket: this.state.ticket
    });
  }

  keyPress(e){
    if (e.keyCode ===13){
      this.handleSubmit();
    }
  }

  render() {
    return (
      <div className="App">
        <header className="App-header">
          <input type="text" placeholder="ENTER TICKET NUMBER" value={this.state.ticket} onKeyDown={this.keyPress} onChange={this.changeTicket}/>
        </header>
      </div>
    );
  }
}

and i'd like to be able to store the updatedTicket value in a variable which I can use in my PassTicket component. 并且我希望能够将updatedTicket值存储在可以在PassTicket组件中使用的变量中。 this is what i've attempted so far but the error it occurs is the following Uncaught TypeError: Cannot read property 'updatedTicket' of undefined 这是到目前为止我尝试过的操作,但是发生的错误是以下Uncaught TypeError: Cannot read property 'updatedTicket' of undefined

this is what my second component looks like: 这是我的第二个组件的样子:

class PassTicket extends Component {

  transferredTicket(){
    const myTicket = this.props.state.updatedTicket;
    return myTicket
  }

  render() {
    return (
        <p>{this.transferredTicket()}</p>
    );
  }
}

When passing down a property from a parent to a child component, the property will be stored onto the props by the name it's passed through. 当将属性从父级传递到子级组件时,该属性将通过其传递的名称存储在道具上。 For example: 例如:

class Parent extends Component {
  state = {
     ticket: '',
  }

  render() {
    return <ChildComponent updatedTicket={this.state.ticket} />
  }
}


class ChildComponent extends Component {
  static propTypes = {
     updatedTicket: PropTypes.string,
  }

  static defaultProps = {
     updatedTicket: '',
  }

  render() {
    return (
      <div>{this.props.updatedTicket}</div>
    );
  }

}

In the example you've given, it doesn't seem like you're passing the state down to the component you're trying to access it in. In addition, it seems like you're trying to access the updatedTicket as a property of a state object, so just beware of how you're accessing your props. 在您给出的示例中,似乎没有将状态传递给要访问其的组件。此外,似乎您正在尝试以属性访问updatedTicket state对象,因此请当心您如何访问道具。

Therefore, in order to access the updatedTicket property on the child component, you'll first need to import the PassTicket component, instantiate it in the parent ( App ) component, and pass the property down: 因此,为了访问子组件上的updatedTicket属性,您首先需要导入PassTicket组件,在父组件( App )中实例化它,然后将该属性向下传递:

<PassTicket updateTicket={this.state.ticket} />

You would then be able to access the string in the PassTicket component like so - this.props.updateTicket 然后,您将可以像这样访问PassTicket组件中的字符串this.props.updateTicket

So .state in react is a local state that is only visible to the individual component. 因此,react中的.state是仅对单个组件可见的局部状态。 You can read more about it here: https://reactjs.org/docs/state-and-lifecycle.html 您可以在此处了解更多信息: https : //reactjs.org/docs/state-and-lifecycle.html

In order to pass your state around, you need to use the props system. 为了传递您的状态,您需要使用道具系统。 So where you instantiate your component, you can pass in the state of the parent. 因此,在实例化组件的位置,您可以传递父级的状态。 For example: 例如:

<PassTicket ticket={this.state.updatedTicket}/>

Then inside your PassTicket render function, you can access the ticket prop: 然后在您的PassTicket渲染函数中,您可以访问票证道具:

render() {
  const { ticket } = this.props
  return (
    <div>{ticket}</div>
  )
}

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

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