简体   繁体   English

通过从子组件到父组件的函数来反应Redux传递参数

[英]React Redux passing parameters via functions from child components to parent

I asked a question on this and I believe Dennis who answered the question gave the answer I thought was correct. 我对此提出了一个问题 ,我相信回答该问题的丹尼斯给出了我认为正确的答案。 I cant figure out how to pass variables back to the parent function correctly... 我无法弄清楚如何将变量正确地传递回父函数...

Here is the code I want to fix. 这是我要修复的代码。

Child of a Child Component 子组件的子组件

First there is a child of a child component. 首先,有一个子组件的子组件。 This will set the sort direction of the list if you click on it. 如果单击列表,将设置列表的排序方向。 Its using "onSortDirectionChange" function and should set the sort direction by setting a variable "sortDirection". 它使用“ onSortDirectionChange”功能,并应通过设置变量“ sortDirection”来设置排序方向。

const GlyphiconDirectionChange = (props) => {
  const { onSortDirectionChange } = props

  return (
    <span>
      {onSortDirectionChange === 'Ascending' ?
          <span onClick={() => onSortDirectionChange('Ascending')} className='glyphicon glyphicon-sort-by-attributes'></span>
        :
          <span onClick={() => onSortDirectionChange('Descending')} className='glyphicon glyphicon-sort-by-attributes-alt'></span>
      }
    </span>
  )
}

Click on the glyphicon next to the header name and it should change the direction of the list. 单击标题名称旁边的glyphicon,它将更改列表的方向。 Simple component. 简单的组件。

Child component 子组件

Its parent, but the child of "ClientContainer" is TableHeaders. 它的父级,但“ ClientContainer”的子级是TableHeaders。 "GlyphiconDirectionChange" sits in this component to display the up or down glyphicon that you can click... “ GlyphiconDirectionChange”位于此组件中,以显示您可以单击的向上或向下的glyphicon。

     <GlyphiconDirectionChange onSortDirectionChange={onSortDirectionChange} />

Parent Component 父组件

ClientContainer is the top component (actions etc will be split off later - they are in there now for simplicity). ClientContainer是最重要的组件(操作等稍后将分拆-为了简单起见,它们现在位于其中)。 In here TableHeaders is displayed as follows: 在这里,TableHeaders显示如下:

        <TableHeaders onSortByChange={this.handleSortBy}
        onSortDirectionChange={this.handleSortDirection}
        query={query}
        currentPage={currentPage}

Note the second line is the function from the bottom component. 注意第二行是底部组件的功能。

It is referring to "HandleSortDirection". 它指的是“ HandleSortDirection”。

  handleSortDirection = (values = {}) => {
  const { sortDirection, query, sortBy  } = this.props
  values.query = values.query || ''
  const searchParams = {
    query,
    sortBy,
    sortDirection,
    ...values,
    currentPage: 1,
  }
  console.log('changeHeaders()!', values, searchParams)
  this.fetchClients(searchParams)
}

Problem How do you set variables in the "child of a child component" and move them through to the "HandleSortDirection" function. 问题如何在“子组件的子组件”中设置变量,然后将其移动到“ HandleSortDirection”函数中。

I am getting the error 我收到错误

Uncaught TypeError: Cannot create property 'query' on string 'Descending' 未捕获的TypeError:无法在字符串“ Descending”上创建属性“ query”

I need the "searchParams" variable "sortDirection" to be set to either "Ascending" or "Descending" and be used correctly in my function.. 我需要将“ searchParams”变量“ sortDirection”设置为“ Ascending”或“ Descending”并在我的函数中正确使用。

How do I pass my sortDirection variable correctly to the function in the parent. 如何将我的sortDirection变量正确传递给父级中的函数。

"How do you set variables in the "child of a child component" and move them through to the "HandleSortDirection" function." “如何在“子组件的子组件”中设置变量,然后将其移动到“ HandleSortDirection”函数中。”

You are already doing this but you have a typo. 您已经在执行此操作,但是有错字。

values.query = values.query || ''

This line is erroring because values is equal to 'Descending' . 这行错误,因为values等于'Descending' You are passing in a string here from your child of child component, and it is being passed up the chain of callbacks til it gets here. 您正在从子组件的子组件中传入一个字符串,并一直沿回调链传递直到到达此处。

Remove that line and replace it with const direction = values; 删除该行,并用const direction = values;代替const direction = values; and then pass direction in place of sortDirection, in searchParams. 然后在searchParams中传递direction代替sortDirection,

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

相关问题 通过函数参数将属性从动态子组件传递到父功能组件 react-native - Passing properties from a dynamic Child component to a Parent functional component through a functions parameters react-native 在react-redux中将函数从子组件传递给父组件 - Passing function from child to parent component in react-redux React Native-Redux-将值从子级传递给父级中的函数 - React Native - Redux - Passing Value from child to function in Parent React JS-子组件的传递函数 - React JS - Passing functions for child components 通过onClick反应将数据从子组件传递到父组件 - React Passing Data From Child Component to Parent Component via a onClick 通过道具将值从React Stateless Child传递给Parent - Passing Values via Props From React Stateless Child to Parent 将数据从子级传递到父级组件-反应-通过回调函数 - passing data from child to parent component - react - via callback function 在 React 中通过 function 从父组件传递到子组件? - Passing function via props from Parent to Child component in React? React Redux在多个级别的组件之间传递多个参数 - React Redux passing multiple parameters across multiple levels of components React Redux-父状态发生变化,但子组件未重新呈现 - React redux - parent state changing, but child components not re-rendering
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM