【问题标题】:React On State Change, Change style对状态变化做出反应,改变风格
【发布时间】:2021-06-06 20:49:54
【问题描述】:

我真的很难过,现在已经尝试了一个星期了尝试了各种各样的东西,但无法让它发挥作用。我真的在互联网上找不到任何东西,所以请帮助我,即使这可能是一个特定的问题

我想要实现的目标: 我想用 Pending 来“补偿” text1 或 text2 的空行。这些行有时是空的,有时是文本。它们在不同组件中的上传时发生变化。 因此,如果 text1 为空,我想将 1 添加到 this.state.count

我为什么要打印一本手册,格式非常重要。通常周围有更多代码,但我将其删除以使其更容易理解

import React, { Component } from "react";
import axios from "axios";


class BspComponent extends React.Component {

constructor(props) {
super(props);

this.state = {
  karte: [],
  text1: "",
  text2: "",
  count: 0,
};
}

componentDidMount() {

axios
  .get("http://test.com")
  .then((response) => {
    this.setState({
      karte: response.data,
      text1: response.data.z1,
      text2: response.data.z2,
    });
    
  })
  .catch((error) => {
    console.log(error);
  });
}


render() {
return (
  <div>
   <div
      className="atDistance" /** This Componant should get a padding is text1 is empty */
      style={{
        paddingTop: `${this.state.count}cm`,
      }}
    >
      BLA BLA
    </div>
    <div className="wrapper" /** text1 and 2 change on an Upload in a difrent Component */>
      <p>{this.state.text1}</p>  
      <p>{this.state.text2}</p>
    </div>
    <div
      className="atDistance" /** This Componant should get a padding is text1 is empty */
      style={{
        paddingTop: `${this.state.count}cm`,
      }}
    >
      BLA BLA
    </div>
  </div>
 );
 }
}

export default BspComponent;

我尝试了什么:

  1. 检查渲染 -> 没有工作,因为我无法达到状态 + 在状态渲染之前有填充组件

  2. 签入 ComponentDidMount ->不知道为什么没有工作

  3. 使用 onChange -> 无法调用该函数时出现错误,因为我陷入了无限循环

如果您有一个很好的想法来处理这个问题,那就太酷了。我哪儿也去不了

【问题讨论】:

    标签: reactjs state setstate


    【解决方案1】:

    如果您只是担心有填充,那么您可以执行类似的操作。

    paddingTop: `${this.state.text ? 1 : 0 }cm`,
    

    并将其应用于您的元素。

    import React, { Component } from "react";
    import axios from "axios";
    
    
    class BspComponent extends React.Component {
    
    constructor(props) {
    super(props);
    
    this.state = {
      karte: [],
      text1: "",
      text2: "",
      count: 0,
    };
    }
    
    componentDidMount() {
    axios
      .get("http://test.com")
      .then((response) => {
        this.setState({
          karte: response.data,
          text1: response.data.z1,
          text2: response.data.z2,
        });
        
      })
      .catch((error) => {
        console.log(error);
      });
    }
    
    
    render() {
    return (
      <div>
       <div
          className="atDistance" /** This Componant should get a padding is text1 is empty */
          style={{
            paddingTop: `${this.state.text ? 1 : 0 }cm`,
          }}
        >
          BLA BLA
        </div>
        <div className="wrapper" /** text1 and 2 change on an Upload in a difrent Component */>
          <p>{this.state.text1}</p>  
          <p>{this.state.text2}</p>
        </div>
        <div
          className="atDistance" /** This Componant should get a padding is text1 is empty */
          style={{
            paddingTop: `${this.state.text ? 1 : 0 }cm`,
          }}
        >
          BLA BLA
        </div>
      </div>
     );
     }
    }
    
    export default BspComponent;
    

    如果你想在收到空文本时将计数增加 1,那么你可以这样做。

    import React, { Component } from "react";
    import axios from "axios";
    
    
    class BspComponent extends React.Component {
    
    constructor(props) {
    super(props);
    
    this.state = {
      karte: [],
      text1: "",
      text2: "",
      count: 0,
    };
    }
    
    componentDidMount() {
    const { count } = this.state;
    axios
      .get("http://test.com")
      .then((response) => {
        this.setState({
          karte: response.data,
          text1: response.data.z1,
          text2: response.data.z2,
          count: response.data.z1 ? count + 1 : count;
        });
        
      })
      .catch((error) => {
        console.log(error);
      });
    }
    
    
    render() {
    return (
      <div>
       <div
          className="atDistance" /** This Componant should get a padding is text1 is empty */
          style={{
            paddingTop: `${this.state.count}cm`,
          }}
        >
          BLA BLA
        </div>
        <div className="wrapper" /** text1 and 2 change on an Upload in a difrent Component */>
          <p>{this.state.text1}</p>  
          <p>{this.state.text2}</p>
        </div>
        <div
          className="atDistance" /** This Componant should get a padding is text1 is empty */
          style={{
            paddingTop: `${this.state.count}cm`,
          }}
        >
          BLA BLA
        </div>
      </div>
     );
     }
    }
    
    export default BspComponent;
    

    如果你想要别的东西,请告诉我。

    【讨论】:

    • 感谢您的快速回答。这是一个很好的解决方案,但我有多行(大约 16 行)要检查(text1,text2,...)。对于每个空行,要将 Counter 加 1,然后将 Counter 作为 Padding。
    猜你喜欢
    • 2020-05-18
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-22
    • 2019-07-25
    • 1970-01-01
    相关资源
    最近更新 更多