如何使React HOC - 高阶组件协同工作?

Srg*_*man 8 javascript reactjs

我有一个像这样的小型演示组件:

function MyList({data, uppercaseMe, lowercaseMe}) {
    return <ul>
        {data.map(item =>
            <li>{item} -
                <button onClick={() => uppercaseMe(item)}>Uppercase me!</button>
                <button onClick={() => lowercaseMe(item)}>Lowercase me!</button>
            </li>)}
    </ul>;
}
Run Code Online (Sandbox Code Playgroud)

然后我想用三个HOC来装饰MyList:

const WithData = (Component) => {
return class extends React.Component {

    constructor(props) {
        super(props);
        this.state= {data:['one', 'two', 'three', 'four', 'five']};
    };

    render() {
        return <Component {...this.props} data={this.state.data} />;
    }
}
};

const WithUppercase = (Component) => {
return class extends React.Component {

    uppercaseMe = (item) => {
        // how to access WithData state from here?
        console.log(item.toUpperCase());
    };

    constructor(props) {
        super(props);
    };

    render() {
        return <Component {...this.props} uppercaseMe={this.uppercaseMe}/>;
    }
 }
};

const WithLowercase = (Component) => {
return class extends React.Component {

    lowercaseMe = (item) => {
        // how to access WithData state from here?
        console.log(item.toLowerCase());
    };

    constructor(props) {
        super(props);
    };

    render() {
        return <Component {...this.props} lowercaseMe={this.lowercaseMe}/>;
    }
 }
};
Run Code Online (Sandbox Code Playgroud)

像这样:

const ComposedList = WithLowercase(WithUppercase(WithData(MyList)));
Run Code Online (Sandbox Code Playgroud)

不幸的是,我不知道如何在WithLowercase或WithUppercase中更改WithData的状态.

你如何将一个HOC的状态暴露给链中的另一个HOCS?

Fur*_*usD 8

您不希望直接公开状态.你想要做的是将一个函数作为prop传递给包装组件,以允许你更新状态.

因此,如果我们使用HOC并添加一些代码,则创建一个函数来更新其状态,现在可以将其传递给其子组件:

const WithData = (Component) => {
  return class extends React.Component {

      constructor(props) {
          super(props);
          this.state= {data:['one', 'two', 'three', 'four', 'five']};
      }

      update (data) {
        this.setState({ data });
      }

      render() {
          return <Component onUpdate={this.update} {...this.props} data={this.state.data} />;
      }
  }
};
Run Code Online (Sandbox Code Playgroud)

现在,在其中一个子组件中,我们可以使用它:

const WithUppercase = (Component) => {
    return class extends React.Component {

        uppercaseMe = (item) => {
            const itemIndex = this.props.data.indexOf(item);
            const newData = this.props.data.slice();
            newData[itemIndex] = item.toUpperCase();
            this.props.onUpdate(newData); // update WithData state here
        };

        constructor(props) {
            super(props);
        };

        render() {
            // onUpdate function is once again passed to child components
            // implicitly through props if you need to call it again
            return <Component {...this.props} uppercaseMe={this.uppercaseMe}/>;
        }
    }
};
Run Code Online (Sandbox Code Playgroud)