代码之家  ›  专栏  ›  技术社区  ›  marius

react typescript传递成员变量

  •  0
  • marius  · 技术社区  · 7 年前

    不过,据我所知,我只能通过道具和状态。我知道我可以通过PROPS或其他参数传递membervariables,但是有没有更漂亮的解决方案呢?

    class Child extends React.Component<any,any> {
        private anyValue:string;
        constructor(props) {
            this.change = this.change.bind(this);
        }
        public render() {
            return (
                <input onChange={this.change} value={this.anyValue}/>
            );
        }
        private change(e:any) {
            this.anyValue = e.target.value;
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Matt McCutchen    7 年前

    这正是州政府的宗旨:

    class Child extends React.Component<any,{anyValue: string}> {
        constructor(props) {
            super(props);
            this.change = this.change.bind(this);
        }
        public render() {
            return (
                <input onChange={this.change} value={this.state.anyValue}/>
            );
        }
        private change(e:any) {
            this.setState({anyValue: e.target.value});
        }
    }
    

    forceUpdate .