代码之家  ›  专栏  ›  技术社区  ›  Nikola Lukic

在react js中堆叠样式

  •  0
  • Nikola Lukic  · 技术社区  · 6 年前

    什么是反应点。我们有道具谁是只读的,我不能编辑它,我们有状态也不能从类外空间编辑?!

    我遵循教程。。。

    这是全班同学:

    import * as React from "react";
    import { CSSProperties } from "react";
    import * as ReactDOM from "react-dom";
    import { Label } from "../../components/label/label";
    import IApp from "../../interfaces/global-interfaces";
    import Services from "../../services/services";
    import { HeaderI, HeaderStateI } from "./header-interface";
    // import { myStyle } from "./style";
    
    enum myEventList {
       iNeedSomeUpdate = "i-need-some-update",
    }
    
    export class Header extends React.Component< HeaderI, HeaderStateI , any > {
    
      public myEvent = Services.CreateEvent(myEventList.iNeedSomeUpdate, {self: this} );
      public myRef: React.RefObject<HTMLDivElement>;
      public myDOM: Element | Text;
    
      private myStyle: IApp.MyMinimumCssInterface = {
        display: "block",
        background: "#559d96",
        height: "100px",
        textAlign: "center",
      };
    
        constructor(args: any) {
            super(args);
    
            this.state = { enabledComponent : true,
                           visibility: true,
                           debugView: false,
                           background: args.background,
                           elements: [],
                           // tslint:disable-next-line:object-literal-shorthand
                           myStyle: this.myStyle,
                        };
    
            // e.detail.data.self..background = this.state.background;
    
            this.myRef = React.createRef();
            this.add = this.add.bind(this);
    
        }
    
        // Override func
        public componentDidMount() {
    
          this.myDOM  = this.myRef.current;
          this.myDOM.addEventListener(myEventList.iNeedSomeUpdate, this.updateOnMyEvent);
    
        }
    
        public updateOnMyEvent(e: CustomEvent) {
    
          e.detail.data.self.printMe();
          console.log("My custom event is done!");
          e.detail.data.self.adapt();
    
        }
    
        public printMe() {
          console.log("Layout Header is active and update is on");
        }
    
        public render() {
    
            if ( this.state.debugView === false ) {
    
              return (
                    <div ref={this.myRef} style={this.state.myStyle} onClick={this.TestEvent.bind(this)} >
                    <Label name="headerName" text="i am header paragraph!" />
                    {this.state.elements.map((i: any) => {
                      return <span key={i} >{i}</span>;
                    })}
                    </div>
                  );
    
            } else {
    
                this.printMe();
    
                return (
                    <div style={this.state.myStyle} ref={this.myRef} >
                    <Label name="headerName" text="i am header paragraph!"/>
                    {this.state.elements.map((i: any) => {
                       return <li key={i} >{i}</li>;
                    })}
                    </div>
                );
    
            }
    
        }
    
        public componentDidUpdate(prevProps: any) {
    
            // Typical usage (don't forget to compare props):
            console.warn("prevProps name is: " + prevProps.name);
            if (this.props.background !== prevProps.background) {
              this.printMe();
            } else {
                console.log("Background is same no update.");
            }
    
        }
    
        public add = (id: number, content: any, event: any ) => {
    
          let localArr: any[] = [];
          localArr = this.state.elements;
          localArr.push(React.createElement("div", { key: id , onClick : null }, content));
    
          this.setState(
            {
              elements: localArr,
              visibility : false,
            },
          );
    
          // tslint:disable-next-line:no-unused-expression
          console.log(" add from class in state elements,  visible is " , this.state.visibility );
    
        }
    
        public TestEvent(event: MouseEvent) {
    
          this.add( 1 , "fffff", null);
          this.add( 2 , "zzzzzz", null);
    
          this.myDOM.dispatchEvent(this.myEvent);
    
        }
    
        public adapt() {
    
          this.myStyle.background = "lime";
    
          this.setState({
            myStyle: this.myStyle,
          });
    
        }
    
    }
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   stef    6 年前

    因为myStyle是“冻结”的,所以需要克隆对象,进行更改,然后使用 setState .

    public adapt() {
      const {myStyle} = this.state
      let newMyStyle = {...myStyle}
      newMyStyle.background = "lime";
    
      this.setState({
        myStyle: newMyStyle,
      });
    
    }
    
        2
  •  0
  •   froston    6 年前

    several ways 如何管理,即。

      const myStyle = Object.assign({}, this.state.myStyle, { background: "lime" })
      this.setState({ myStyle })