简体   繁体   English

无法在 componentDidMount 中使用来自父组件的道具

[英]Can't use props from parent component in componentDidMount

I'm trying to send props from parent component to child component.我正在尝试将props从父组件发送到子组件。 I want to use this in componentDidMount() at child component.我想在子组件的componentDidMount()中使用它。 But it can't get this value.但是它不能得到这个值。 It can get this in render() although.虽然它可以在render()得到它。

parent component code:父组件代码:

class Parent extends Component {
  componentDidMount() {
    const { id } = this.props.match.params;
    // fetch with redux
    this.props.fetchItem(id);
  }

  renderItem() {
    const { item } = this.props;
    console.log("item", item);
    // item exists
    return <Child item={item} />
  }

  render() {
    return (
      <div>
       {this.renderItem()}
      </div>
    )
  }
}

child component code:子组件代码:

class Child extends Component {
  componentDidMount() {
    const { item } = this.props;
    console.log("item", item);
    // item doesn't exist
  }

  render() {
    console.log("item", item)
    // item exists
    return (
      <p>{item.name}</p>
    )
  }
}

If you want to pass props to a child component, this is how you would do it.如果您想将 props 传递给子组件,您可以这样做。

import MyChildComponent from './itspath'; // import your child component

class Whatever extends React.Component {
    const name = "Bob";      // Pay attention this variable we will pass as props

    <MyChildComponent name={name} />   // Here name is now a prop
}
export default Whatever;

Now to use the props.现在来使用道具。 You do not need a class to use the props, but since that is what you were trying.你不需要一个类来使用道具,但因为那是你正在尝试的。

class MyChildComponents extends React.Component {
   componentDidMount() {
  const propsSent  = this.props.name;
  console.log(propsSent);     // This will get you the prop name with a value of Bob
}
}

暂无
暂无

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

相关问题 componentDidMount无法从父组件接收道具 - componentDidMount can't receive props from parent component 在子组件中的componentDidMount中未定义来自异步父组件的道具数组 - Props array from async parent component undefined when in componentDidMount in child component 如何从子组件 Class 中的父组件访问道具? (专门针对 componentDidMount)? - How to access props from Parent Component inside the Child Class Component ? (specifically for componentDidMount)? 您可以在不扩展组件的 React class 上使用 componentDidMount() 吗? - Can you use componentDidMount() on a React class that doesn't extend Component? 如何过滤Next.js中的道具? 无法从 Next.js 中的 componentDidMount 过滤道具中的数据 - How to filter props in Next.js? Can't filter data in props from componentDidMount in Next.js 使用 componentDidMount 和 componentWillRecieveProps 方法无法从父级获取道具 testHighlight - using componentDidMount and componentWillRecieveProps methods not able to fetch the props testHighlight from parent 将道具传递给React组件中的componentDidMount() - Pass props to componentDidMount() in React component React 父组件道具无法在子组件中使用 forwardRef 道具访问 - React parent component props can't access using forwardRef props in child component 将{t}(从i18n-react)传递到无状态组件时,不能使用props - Can't use props when passing { t } (from i18n-react) to stateless component 我们可以在 React 中将 props 从 Parent 传递给 Child 组件,并将 props 设置为子组件的 state 吗? - Can we pass props from Parent to Child component in React, and set the props as state of child component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM