简体   繁体   English

当更改作为数组传递的道具时,React功能组件不会重新渲染

[英]React functional component doesnt rerender when changing props passed as an array

My functional Component doesn't rerender when i pass an array as it's props and change the values of the array. 当我传递数组作为道具并更改数组的值时,我的功能组件不会重新渲染。

https://codesandbox.io/s/5ym4vkrnnk https://codesandbox.io/s/5ym4vkrnnk

or 要么

import React, { PureComponent, Fragment } from "react";
import ReactDOM from "react-dom";

const SomeFunctionalComponent = props => {
  return <span>{props.array[0]}</span>;
};

class App extends PureComponent {
  state = {
    array: [1, 2, 3]
  };
  increase = () => {
    var array = this.state.array;
    array[0] = array[0] + 1;
    this.setState({ array: array });
    console.log(this.state.array);
  };
  render() {
    return (
      <Fragment>
        <button onClick={this.increase}>Increase</button>
        <SomeFunctionalComponent array={this.state.array} />
      </Fragment>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

My questions: 我的问题:

  1. Why is that so? 为什么会这样?
  2. How can i change that? 我该如何更改?

Thanks for any help in advance! 感谢您的任何帮助!

Never Mutate the state directly 从不直接改变状态

you are mutating the state directly in your code so the changes are not getting reflected. 您直接在代码中更改状态,因此更改不会得到反映。 Array becomes a reference to this.state.array, which causes the undesired mutation. Array成为对此this.state.array的引用,这会导致意外的突变。

import React, { PureComponent, Fragment } from "react";
import ReactDOM from "react-dom";

const SomeFunctionalComponent = props => {
  return <span>{props.array[0]}</span>;
};

class App extends PureComponent {
  state = {
    array: [1, 2, 3]
  };
  increase = () => {
    var array = [...this.state.array];
    array[0] = array[0] + 1;
    this.setState({ array: array });
    console.log(this.state.array);
  };
  render() {
    return (
      <Fragment>
        <button onClick={this.increase}>Increase</button>
        <SomeFunctionalComponent array={this.state.array} />
      </Fragment>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

暂无
暂无

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

相关问题 更改props值时,React不重新渲染组件 - React doesnt rerender component when props value is being change 道具更改时重新渲染React组件? - Rerender React component when props change? React JS 道具不会在父重新渲染时传递给子组件 - React JS props are not passed to child component on parent rerender 当传递到组件的道具时,反应钩子数组传递数字 - React hooks array passing in number when passed into props of component 当 Select Option Value 改变时触发 Functional React 组件的重新渲染 - Trigger rerender of Functional React Component when Select Option Value is changed Typescript React 无法识别传递给功能组件的道具,ts 2740 - Typescript React fails to identify props passed to functional component, ts 2740 检查从功能状态redux组件中的mapStateToProps传递的道具的真实性 - Check truthiness of props passed from mapStateToProps in functional react redux component 当传递给反应中的功能组件时,Prop 是未定义的 - Prop is undefined when passed to functional component in react ReduxToolkit useSelector Hook:通过 useDispatch 更新选定的 redux-state 后,React 功能组件不会重新渲染 - ReduxToolkit useSelector Hook: React functional component doesnt rerender after selected redux-state is updated through useDispatch React - 如何从正在更改的对象数组中仅重新渲染一个组件 - React - How to rerender only one component from an array of objects that is changing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM