简体   繁体   English

React 道具值未定义

[英]React props value is undefined

This is my parent code:这是我的父代码:

class Parent extends Component {
    constructor(props) {
        super(props);
        this.state = {
            tags: [],
        };
    }
    componentDidMount() {
        this.getTags();
    }

    getTags() {
        //method gets tags from the backend
    }
    render() {
        return <Child tags={this.state.tags} />;
    }
}

And this is basically my child component:这基本上是我的子组件:

export default class Child extends Component {
    constructor(props) {
        super(props);
        this.state = {
            tags: props.tags,
        };
    }

    componentWillReceiveProps(nextProps) {
        this.setState({
            tags: nextProps.tags,
        });
    }
}

But when I console log tags somewhere in the Child component, it is undefined.但是,当我在Child组件的某处控制日志标签时,它是未定义的。 Maybe it is undefined because the child component gets rendered before the parent component calls the method getTags ?也许它是未定义的,因为子组件在父组件调用方法getTags之前被渲染? Or is there any other problem with this code?或者这段代码还有其他问题吗? And how can I avoid this problem that tags are undefined in the child component?以及如何避免子组件中未定义标签的问题?

Cheers干杯

To avoid your problem, you shouldn't be rendering your Child component until the this.state.tags has any useful values.为避免您的问题,在this.state.tags具有任何有用值之前,您不应渲染您的Child组件。

Here is how you can do it and also show a "Loading..." text, so the user isn't worried the page is broken.这是您如何做到这一点并显示“正在加载...”文本,因此用户不必担心页面损坏。

class Parent extends Component {
    constructor(props) {
        super(props);
        this.state = {
            tags: [],
        };
    }
    componentDidMount() {
        this.getTags();
    }

    getTags() {
        //method gets tags from the backend
    }
    render() {
        return this.state.tags.length ? (
            'Loading...'
        ) : (
            <Child tags={this.state.tags} />
        );
    }
}

Your child component will definitely get rendered with the empty 'tags' array as a prop.您的子组件肯定会使用空的“标签”数组作为道具进行渲染。 Then, when getTags() returns the data, the newly populated tags array will be passed to the child as a prop, forcing the child to get re-rendered with the new data.然后,当 getTags() 返回数据时,新填充的标签数组将作为道具传递给孩子,强制孩子用新数据重新渲染。

It should be the empty array though, not "undefined".它应该是空数组,而不是“未定义”。 You might check your getTags() method and the API you are calling to make sure you aren't getting "undefined" from there.您可以检查您的 getTags() 方法和您正在调用的 API 以确保您没有从那里得到“未定义”。

componentWillReceiveProps is legacy and should not be used. componentWillReceiveProps 是遗留的,不应使用。 See the following link in the React docs for details: https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops有关详细信息,请参阅 React 文档中的以下链接: https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops

That documentation will walk you through what to do if you need to perform side effects as a result of changing props.如果您因更改道具而需要执行副作用,该文档将指导您如何做。

Right now the only thing is componentWillReceiveProps is to set local state to the props, which is totally superfluous.现在唯一要做的就是 componentWillReceiveProps 就是将本地 state 设置为 props,这完全是多余的。 Is there something else you are needing to do there?您还需要在那里做其他事情吗?

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

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