小编Car*_*ual的帖子

componentWillReceiveProps没有触发

在我的其他类中,componentWillReceiveProps运行良好,但由于某种原因,它不会在这里触发.

ItemView.jsx

class ItemView extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            name: null, 
            rating: null, 
            sector: null, 
            location: null, 
            description: null, 
            image: "blank.png",
            show: false
       };
    }

    ...

    componentWillReceiveProps(nextProps) {

        if(!nextProps.companyToRender) {
            this.setState({
                name: null, 
                rating: null, 
                sector: null, 
                location: null, 
                description: null, 
                image: "blank.png",
                show: false
            });
       }
       else {
           var companyToRender = nextProps.companyToRender;

           this.setState({
               name: companyToRender.name, 
               rating: companyToRender.rating, 
               sector: companyToRender.sector, 
               location: companyToRender.location, 
               description: companyToRender.description, 
               image: companyToRender.image,
               show: true
           });
    }

    ...

    render () {
       return(
        <div> …
Run Code Online (Sandbox Code Playgroud)

reactjs react-jsx

24
推荐指数
3
解决办法
3万
查看次数

标签 统计

react-jsx ×1

reactjs ×1