【问题标题】:How to pass componentDidMount function to deep layer children in React如何将componentDidMount函数传递给React中的深层子级
【发布时间】:2018-09-07 14:16:47
【问题描述】:

我对将 componentDidMount 函数从父级传递给深层子级有疑问。 我有一个项目列表,由项目状态选择。更改其中一项的状态后,我需要重新渲染父项以获取新数据。对我来说棘手的部分是,我找不到方法,如何传递 componentDidMount 函数或操作来再次获取我的列表数据。

我的父类:

class Page extends React.Component {
  componentDidMount() {
    this.props.onCompMount();
  }

  render() {
    const { error, loading, list } = this.props;

    const pageListProps = {
      loading,
      error,
      list,
    };

    return (
      <article>
        <div>
          <PageList {...pageListProps} />
        </div>
      </article>
    );
  }
}

我的第一个孩子:

function PageList({ loading, error, list }) {
  if (loading) {
    return <List component={LoadingIndicator} />;
  }

  if (error !== false) {
    const ErrorComponent = () => (
      <ListItem item="Something went wrong, please try again!" />
    );
    return <List component={ErrorComponent} />;
  }

  if (list !== false) {
    return <List items={list} component={PageItem} />;
  }
  return null;
}

第二个孩子:

export class PageItem extends React.PureComponent {
  constructor() {
    super();

    this.state = {
      modalIsOpen: false,
    };

    this.openModal = this.openModal.bind(this);
    this.closeModal = this.closeModal.bind(this);
  }

  openModal() {
    this.setState({ modalIsOpen: true });
  }

  closeModal() {
    this.setState({ modalIsOpen: false });
  }

  render() {
    const { item } = this.props;

    // Put together the content of the repository
    const content = (
      <Wrapper>
        <h3>{item.title}</h3>
        <button onClick={this.openModal}>Decline</button>
        <Modal
          isOpen={this.state.modalIsOpen}
          onRequestClose={this.closeModal}
          style={customStyles}
          contentLabel="Preview"
        >
          <Form close={this.closeModal} />
        </Modal>
      </Wrapper>
    );

提交后我想要重新渲染父容器的最后一个孩子:

export class Form extends React.Component {

  render() {
    return (
      <article>
        <form
          onSubmit={e => {
            e.preventDefault();
            this.props.submit();
            this.props.close();
            //Somehow re-render parent
          }}
        >
          <div className="row" style={{ textAlign: 'start' }}>
            Do you really want to change status?
            <div className="col-md-12 buttonContainer">
              <ButtonA
                label="Submit"
                style={{ width: '50%' }}
                primary
                type="submit"
              />
            </div>
          </div>
        </form>
      </article>
    );
  }
}

我尝试使用window.location.reload(); 重新加载页面,它可以工作。但我认为使用 React 是不好的做法。也许有人可以建议我如何让它变得更好?

编辑:我正在添加父减速器和第 4 个子减速器。

父级减速器:

const initialState = fromJS({
  loading: false,
  error: false,
  listData: {
    list: false,
  },
});

function pageReducer(state = initialState, action) {
  switch (action.type) {
    case FETCH_LIST_BEGIN:
      return state
        .set('loading', true)
        .set('error', false)
        .setIn(['listData', 'list'], false);
    case FETCH_LIST_SUCCESS:
      return state
        .setIn(['listData', 'list'], action.list)
        .set('loading', false);
    case FETCH_LIST_FAILURE:
      return state.set('error', action.error).set('loading', false);
    default:
      return state;
  }
}

export default pageReducer;

第 4 个子减速器:

const initialState = fromJS({});

function formReducer(state = initialState, action) {
  switch (action.type) {
    case SUBMIT:
      return state;
    default:
      return state;
  }
}

export default formReducer;

【问题讨论】:

  • 你使用 redux 来获取?
  • 您要更新哪个孩子的物品状态?
  • @SakhiMansoor,最后一个孩子有 redux-saga POST 方法,我将更新状态(列表对象)发送到后端。
  • 必须在父组件中获取数据
  • 您可以从父级调度操作并将减速器与第 4 个 lvl 子级连接。

标签: reactjs react-redux react-boilerplate


【解决方案1】:

我们使用Redux 或 React 的新 Context API 来避免 React 中的支柱钻孔问题。 在您的使用中,您可以从父组件调度操作并将相关的减速器连接到您的第 4 级子组件。因此,当您的 reducer 更新全局状态(存储)时,您连接的组件将重新渲染并获取更新后的状态,就像在 props 中一样。

【讨论】:

    【解决方案2】:

    只需将this.props.onCompMount() 作为道具传递给子组件,然后每次更新时在子组件中调用它。

    【讨论】:

    • 是的,如果我需要将功能从父母传递给孩子,我会这样做。现在我需要将函数传递给第 4 个 lvl 孩子。
    • 您可以将其作为道具从第 1 级传递到第 2 级子级,然后从第 2 级传递到第 3 级,然后从第 4 级传递到第 3 级。
    • 也许你可以告诉我如何使用我的代码来做到这一点?我做不到。
    猜你喜欢
    • 2021-02-03
    • 2020-08-23
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多