简体   繁体   English

React中从父组件传递给子组件的“未定义”?

[英]'Undefined' passed from parent to child component in React?

I have the following code with a parent and a child class, The parent class is the Container and the child presents the results on the screen.However I am unable to pass values from parent to child.我有以下代码与父和子 class,父 class 是容器,孩子在屏幕上显示结果。但是我无法将值从父母传递给孩子。 Whenever I try to pass the value, I get undefined每当我尝试传递值时,我都会得到未定义

Parent Class父 Class

class Container extends React.Component{
    constructor(props) {
        super(props);
        this.state = {course:"Commerce",grade:"10",btnState:"Add"}
}
render(){

        return (
          <Presentation course={this.state.course} grade={this.state.grade} btnState={this.state.btnState}
           />
        );
    }
}

Child Class儿童 Class

class Presentation extends React.Component {
     debug(){
       alert(this.props.course)
     }
    render () {
  return (
    <div>

        <form>

            <input type="text" value={this.props.course} /><br/><br/>
            <input type="text" value={this.props.grade}  /><br/><br/>
            <input type="reset" id="addCourse" value={this.props.btnState} 
  onClick={this.debug.bind(this)}/>
            </form>
    </div>
  );
}
   }

The Rendering渲染

ReactDOM.render(
      <Presentation />
      , 
      document.querySelector("#container1")
    );

The form is rendering correctly but the values are not being passed into the child from the parent.表单正在正确呈现,但值没有从父级传递给子级。 alert(this.props.course} gives undefined alert(this.props.course}给出未定义

You say you have a container component, but you're directly rendering the child component Presentation .你说你有一个容器组件,但你直接渲染子组件Presentation

 ReactDOM.render( <Presentation />, document.querySelector("#container1") );

Try rendering the container, eg尝试渲染容器,例如

ReactDOM.render(
  <Container />,
  document.querySelector("#container1")
);

And see if that renders the container, creates state, and passes the expected props to Presentation .并查看是否呈现容器,创建 state,并将预期的道具传递给Presentation

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

相关问题 未定义的数据从子组件传递到父组件 - Data passed as undefined from child to parent component 当单击从父组件传递时,为什么子组件未定义“this”? - Why 'this' is undefined for child component, when click is passed from a parent component? React 子组件不会更新从父组件传递的属性 - React Child Component does not update properties passed from Parent Component 从 Angular 父组件传递给子组件的数据在子组件中未定义 - Data passed from an Angular parent component to a child component is undefined in child component 在React中没有从子组件传递给父组件的值 - Value not getting passed from child to parent component in React 为什么反应道具未定义传递给子组件? - why react props are passed undefined to the child component? 传递给子组件的 React 变量未定义 - React variable passed down to child component is undefined 从子组件传递时将 state 保存在父组件中 - 用于多个项目(React Hooks) - saving state in Parent component when passed from the child component- for multiple items(React Hooks) React JS-如何访问从父组件传递的子组件中的道具 - React JS - How to access props in a child component which is passed from a parent component React中的回调参数如何从子组件传递到父组件? - How is an argument for a callback in React being passed from the child component to the parent component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM