简体   繁体   English

当props进入组件时,为什么我的react组件没有重新渲染?

[英]Why is my react component not re rendering when props comes into the component?

I am currently in a project, and I have had to do null checks on every single props that has come in to children components wether through redux or passing it in myself. 我目前正在一个项目中,我必须对通过redux或将其传递给自己的子组件中的每个道具进行空检查。 I feel like that is not normal with react? 我觉得这不正常吗? Isn't a huge plus side of React is automatic re-rendering? React的巨大优势不是自动重新渲染吗? If I try to put anything into state, I can't because There has to be a null check in the render before I do anything with the data. 如果我尝试将任何东西置于状态,那么我就不能这样做,因为在对数据进行任何处理之前,必须在渲染中进行null检查。 Thanks in advance!! 提前致谢!!

PARENT COMPONENT = 父组件=

class App extends Component {

     componentDidMount(){
        //where I load the data
        this.loadCardsFromServer();
        this.props.GetAllData();
      }


      render() {
      //NEED TO DO A NULL CHECK FROM THIS COMING FROM REDUX
         const filteredData = !!this.state.data ? this.state.data.filter(card =>{
         return  card.name.toUpperCase().includes(this.state.input.toUpperCase())
         }) : null;

      return (
      //MAKES ME DO ANOTHER NULL CHECK
         <div>
                  {!!this.state.data ? filteredData.map(i => <Card person={i} key={i.created} planet={this.props.planets} />) : null}
         </div>
   ))}

CHILD COMPONENT OF CARD 儿童卡片

  class Card extends Component {

     //WHERE I WANT TO PUT THE PROPS
     constructor(){
       super();
       this.state={
         edit: false,
         name: this.props.person.name,
         birthYear: this.props.person.birth_year
       }
     }


      render() {
        let world = null;
        //ANOTHER NULL CHECK
        if(this.props.planet){
           this.props.planet.map(i => {
             if(i.id === this.props.person.id){
              world = i.name
        }
    })
  }

  return (
    <div>
        //THIS IS WHERE I WANT THE VALUE TO BE STATE
        {this.state.edit ? <input label="Name" value={this.state.name}/> : <div className='card-name'>{name}</div>}

    </div>

You need to update state when data arrive. 您需要在数据到达时更新状态。 You can do it like this: 您可以这样做:

import React, { Component } from 'react';
import './App.scss';
import Card from './Components/Card/Card.js';

class App extends Component {

constructor(){
    super();
    this.state = {
        loading:true,
        cards:[]
    };
}

componentDidMount(){
    this.loadCardsFromServer();
}

loadCardsFromServer = () => {
    let cardsResponseArray = [];

    // fetch your cards here, and when you get data:
    // cardsResponseArray = filterFunction(response); // make function to filter

    cardsResponseArray = [{id:1,name:'aaa'},{id:2,name:'bbb'}];
    setTimeout(function () {
        this.setState({
            loading:false,
            cards: cardsResponseArray
        });
    }.bind(this), 2000)
};

render() {
    if(this.state.loading === true){
        return(
            <h1>loading !!!!!!!!</h1>
        );
    } else {
        return (
            <div>
                {this.state.cards.map(card => (
                    <Card key={card.id} card={card}></Card>
                ))}
            </div>
        );
    }

    }
}
export default App;

And then in your Card component: 然后在您的Card组件中:

import React, { Component } from 'react';

class Card extends Component {

constructor(props){
    super(props);
    this.props = props;
    this.state = {
        id:this.props.card.id,
        name:this.props.card.name
    };
}

render() {
    return (
        <div className={'class'} >
            Card Id = {this.state.id}, Card name = {this.state.name}

        </div>
    );
}
}

export default Card;

For those interested about React state and lifecycle methods go here 对于那些对React状态和生命周期方法感兴趣的人, 请点击此处

Okay, in this case i craft a little helper's for waiting state in my redux store. 好的,在这种情况下,我在redux商店中为等待状态创建了一个小助手。 I fetch data somewhere (app) and i render a connected component waiting for fetched data in store: 我在某处(应用程序)获取数据,并渲染一个连接的组件,等待存储中获取的数据:

const Loader = (props) => {
  if (!props.loaded) {
      return null;
  }
  <Card data={props.data}/> 
}

const CardLoader = connect (state => {
  return {
    loaded: state.data !== undefined
    data: state.data
   }
 })(Loader)


  <CardLoader />

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

相关问题 为什么在更改发送给子组件(react js)的道具的值时会进行组件重新渲染? - Why is a Component Re-Rendering done when changing the value of the props sent to child Component (react js)? 为什么我的组件没有在响应中重新渲染 - why my component is not re-rendering in react 当道具更改时,React 子组件不会重新渲染 - React Child Component not re-rendering when props change 为什么我的道具在挂载渲染时没有被传递到我的组件中? - Why are my props not being passed into my component when mount rendering? 重新渲染我的 React 组件时,我的动画不起作用? - My animation is not working when re rendering my react component? 为什么我的反应组件不断重新渲染? - Why does my react component keep re-rendering? 将 props 传递给子组件时避免组件重新渲染 - Avoid component re-rendering when passing props to child component 为什么我的组件在作为孩子传入时没有重新渲染? - Why is my component not re-rendering when passed in as a child? 为什么我的React组件没有渲染? - Why my react component is not rendering? 在 React DevTools 中,我的组件正在重新渲染,但父组件没有,并且它的状态/道具没有改变 - In React DevTools my component is re-rendering but the parent is not, and it's state/props are not changing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM