由于状态滥用,阵列无法动态更新

时间:2020-04-12 09:04:11

标签: javascript node.js json reactjs state

单击单选按钮时,它将值从0更改为1,反之亦然。
这是通过2个组件Test.js和Graph_Test.js完成的。
Test.js是创建单选按钮并填充数组的地方。例如,当按下单选按钮1时,我希望它将array [1]中的值从0更改为1或从1更改为0。
因此,每当按下单选按钮时,都会动态更改数组。
然后使用此数组在Graph_Test.js中构建图形,具体取决于哪个索引旁边有1个值,它将在图形上形成相应的线。
例如,如果array = [0,1,0,0],则将为区域1绘制一条线。 因此,随着此数组动态变化,图上的线也会变化。

我正在测试我的代码。在Graph_test中,我输出了array [0],当单选按钮[0]被触摸时,该值应该更新。但是,这没有发生。

在test.js中,我曾经指出这是我的问题所在,因为初始数组被当作prop来使用,但它不是动态更新的。

Graph_test.js有两个通过数组和测试发送的道具,稍后将使用测试来构建图形。但是目前不需要。

我已经尝试了许多尝试,但仍无所获,将不胜感激/需要任何帮助。

代码: Test.js:

// When radio buttons checked, change corresponding value in array if 0 change to 1 and if 1 change to 0
// This will be used in the graph component, and will enable the functionality of selcting one region or multiple region.
// As the graph will be plotted based on which regions are noted 1 in the array 
import $ from "jquery";
import Graph_Test from "./Graph_Test.js";
import React, { useState } from "react";
const Test = props => {
  const total_regions = (JSON.parse(JSON.stringify(props.test)).length); // gets the number of regions
  const [array, setArray] = useState(Array(total_regions.length).fill(0));
    //when a radio button is clicked change its corresponding in the array

//when a radio button is clicked change its corresponding in the array
const handleClick = (item, idx) => {
  if (array[idx] == 1) {
    array[idx] = 0;
  } else {
    array[idx] = 1;
  }
  setArray(array);
};

  return (   // displays radio buttons depending on the number of objects in json
    <div>
      <div>
        <Graph_Test  testing={[]} arrays={array}/>
      </div>
      <div>
        {props.test.map((item, idx) => { 
          return (
            <label key={idx}>
              <input className="region" type="radio" value={idx} onClick={() => handleClick(item, idx)}/>
              <span>{idx}</span> 
            </label>
          );
        })}  
      </div>
    </div>
  );
};
export default Test;

Graph_Test.js

import React from 'react';
import $ from "jquery";
//<h1>{props.testing.map}</h1>
const Graph_Test = props => { 

    return(
      <div>
        <div>
        {props.arrays && props.arrays.length > 0 && <p>{props.arrays[0]}</p> }
        </div>     
      </div >
    );
  };export default Graph_Test;

App.js

import "bootstrap/dist/css/bootstrap.css";
import React from "react";
import ReactPlayer from 'react-player'
import LeftPane from "./components/LeftPane.js";
import Video from "./components/Video.js";
//import Footer from "./components/Footer.js";
import Test from "./components/Test.js";
import Graph_Test from "./components/Graph_Test.js";
//import './App.css';

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { apiResponse: [] };
    this.state = {
      clicked: "no"
    };
  }
  // Comunicate with API
  callAPI() {
    fetch("http://localhost:9000/IntensityAPI") //React app talks to API at this url
      .then(res => res.json())
      .then(res => this.setState({ apiResponse: res }));
  }
  handleClick = () => {
  this.setState({ ...this.state, isClicked: "yes" });
  console.log("clicked");
};
  componentWillMount() {
    this.callAPI();
  }

  render() {
    return (
      <div className="App">
          <div class="row fixed-top fixed-bottom no-gutters"  >
            <div class="col-3 fixed-top fixed-bottom">
              <LeftPane></LeftPane>
            </div>
            <div class="offset-md-3 fixed-top fixed-bottom" >
              <Video></Video>
            </div>
            <div class=" col-3 fixed-bottom">
            <Test test = {this.state.apiResponse} handler={this.handleClick}/>
            <Graph_Test testing = {this.state.apiResponse} arrays={[]}  {...this.state}/>

            </div>      
            </div>

      </div>
    );
  }
}
export default App;
//  <Footer test = {this.state.apiResponse}/>

1 个答案:

答案 0 :(得分:1)

AFAIK React不会对该状态进行深层比较,因此,由于您正在重用已经存在的数组,因此它具有与上一个相同的内存引用。

尝试这样的事情:

const handleClick = (item, idx) => {
  const newArray = [...array]
  if (newArray[idx] == 1) {
    newArray[idx] = 0;
  } else {
    newArray[idx] = 1;
  }
  setArray(newArray);
};