简体   繁体   English

所有clearAllSearchCriteria的值都应传递给NoResult组件

[英]all the values of clearAllSearchCriteria should be passed to NoResult component

  • I am new to react. 我是新来的反应。
  • I am trying to call child method from parent in reactjs. 我试图在reactjs中从父级调用子方法。
  • Here NoResult component is present in SportsCardList component and SportsCardList component is present in Sports-search-favorites-tab 这里,NoResult组件存在于SportsCardList组件中,而SportsCardList组件存在于Sports-search-favorites-tab中
  • so here three different components are involving. 因此这里涉及三个不同的组件。
  • when I click the Clear All Search Criteria div all the values of clearAllSearchCriteria should be passed to NoResult component and from there the values should be passed to Sports-search-favorites-tab.js 当我单击Clear All Search Criteria div时,所有clearAllSearchCriteria的值都应传递到NoResult组件,然后从那里将值传递给Sports-search-favorites-tab.js

  • I used the below link and implemented but I am getting an error TypeError: _this.alfoRef.handleDeleteFromParent is not a function. 我使用下面的链接并实现了,但出现错误TypeError:_this.alfoRef.handleDeleteFromParent不是函数。 call child function from parent in reactjs 在reactjs中从父级调用子级函数

  • can you guys tell me how to pass teh value from child to parent since another component is present in the middle. 你们能告诉我如何将值从子级传递到父级,因为中间有另一个组件。 so that in future I will fix it myself. 这样将来我会自己修复它。
  • providing my relevant code snippets alone below since my codebase is big 由于我的代码库很大,因此仅在下面提供我的相关代码段

TypeError: _this.alfoRef.handleDeleteFromParent is not a function TypeError:_this.alfoRef.handleDeleteFromParent不是函数

granfather component 1 Sports-search-favorites-tab.js 祖父组件1 Sports-search-favorites-tab.js

import SportsCardList from './SportsCardList';

clearAllSearchCriteria = () => {
    console.log('clearAllSearchCriteria');
    this.alfoRef.handleDeleteFromParent();
}

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

    return (
      <Card className={classes.card}>
        <CardContent style={{ paddingTop: 0, paddingBottom: 0 }}>
          <Typography className={classes.noResultContainer}>
            {/*<div className={classes.noResultContainer}>*/}
            <div className={classes.noResultContainerItemText}>No Results Were Returned</div>

            <CardActions className={classes.noResultContainerItem}>
              <div className={classes.clearAll} onClick={this.props.clearAllSearchCriteria} >Clear All Search Criteria</div>
              {/*<div onClick={() => props.clicked(clearAllSearchVar)}/>*/}
              {/*<div onClick={() => props.clicked(clearAllSearchCriteria)}> Clear All Search Criteria</div>*/}


            </CardActions>

            {/*
            <CardActions className={classes.noResultContainerItem}>
              <Button onClick={this.toggleDrawer("right", true)} size="small">Create task for Custom Payment Build</Button>
            </CardActions>*/}

            {/*</div>*/}
          </Typography>
        </CardContent>
        <Drawer
          style={{ width: 500 }}
          anchor="right"
          open={this.state.right}
          onClose={this.toggleDrawer("right", false)}
        >
          <div
            tabIndex={0}
            role="button"
          >
            {/*sideList*/}
            {/*sports advanced search for tab codes */}

            <TabDemo />
          </div>
        </Drawer>
      </Card>


    );

  }

parent 1(middle component2) SportsCardList.js 父级1(中间component2)SportsCardList.js

import NoResult from './no-result';

clearAllSearchCriteria = () =>{
    console.log('clearAllSearchCriteria');
    this.props.clearAllSearchCriteria();
}

<NoResult clearAllSearchCriteria={this.clearAllSearchCriteria}/>

child(grandson) component 3 no-result.js 子级(孙子)组件3 no-result.js

return (
  <Card className={classes.card}>
    <CardContent style={{ paddingTop: 0, paddingBottom: 0 }}>
      <Typography className={classes.noResultContainer}>
        {/*<div className={classes.noResultContainer}>*/}
        <div className={classes.noResultContainerItemText}>No Results Were Returned</div>

        <CardActions className={classes.noResultContainerItem}>
          <div className={classes.clearAll} onClick={this.props.clearAllSearchCriteria} >Clear All Search Criteria</div>
          {/*<div onClick={() => props.clicked(clearAllSearchVar)}/>*/}
          {/*<div onClick={() => props.clicked(clearAllSearchCriteria)}> Clear All Search Criteria</div>*/}


        </CardActions>


        {/*
        <CardActions className={classes.noResultContainerItem}>
          <Button onClick={this.toggleDrawer("right", true)} size="small">Create task for Custom Payment Build</Button>
        </CardActions>*/}

        {/*</div>*/}
      </Typography>
    </CardContent>

  </Card>


);

Grandfather component : 祖父组成部分

clearAllHandler = (clearAllSearchVar) => {
// Work with clearAllSearchVar
}
<Father clicked={this.clearAllHandler} />

Father component : 父亲组成部分

<Son clicked={props.clicked} />

Son component : 子组件

<div onClick={() => props.clicked(clearAllSearchVar)}/>

You should fill clearAllSearchVar in the <Son /> component and whenever you click it the variable is passed to the clearAllHandler function in <Grandfather /> component which passed a reference to the function down to his grand <Son /> through props via the <Father /> . 应填写clearAllSearchVar<Son />组件,每当你点击它的变量传递给clearAllHandler函数<Grandfather />组件,它传递给函数的引用到他的盛大 <Son />经由道具<Father />

Hope that helps! 希望有帮助!

Grandfather: 祖父:

import SportsCardList from './SportsCardList';

clearAllSearchCriteria = (clearAllSearchVar) => {
    //Work here with clearAllSearchVar
    console.log('clearAllSearchCriteria');
    this.alfoRef.handleDeleteFromParent();
}

<div>
    {/*<NoResult />*/}
    <SportsCardList clicked={this.clearAllSearchCriteria}/>

    <Dialog
        isOpen={this.state.open}
        onClose={() => {
            this.setState({ open: false });
        }}
    />
</div>

Father SportsCardList.js: 父亲 SportsCardList.js:

    const SportsCardList = props => {
    <NoResult clicked={props.clicked}/>
}

Son NoResult.js: 儿子 NoResult.js:

const NoResult = props => {
<CardActions className={classes.noResultContainerItem}>
    <div className={classes.clearAll} onClick={() => props.clicked(clearAllSearchCriteria)} >Clear All Search Criteria</div>
</CardActions>
}

Just make sure you fill clearAllSearchCriteria variable. 只要确保您填写clearAllSearchCriteria变量即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM