简体   繁体   English

React Redux在多个级别的组件之间传递多个参数

[英]React Redux passing multiple parameters across multiple levels of components

I had asked this question which threw up more questions regarding the way parameters are passed across components. 我曾问过这个问题 ,这引发了更多关于跨组件传递参数的方式的问题。

Lets say you have three components a bottom or child of a child. 可以说,您有三个组成部分:一个孩子的底部或孩子。 A middle or the child. 中产阶级还是孩子。 and the top - in my case the container. 和顶部-在我的情况下是容器。

I currently have in the bottom component.: 我目前在底部组件中:

const GlyphiconDirectionChange = (props) => {
const { onSortDirectionChange } = props
return (
  <span onClick={() => onSortDirectionChange('Ascending')} className='glyphicon glyphicon-sort-by-attributes'></span>
)

Note the setting of a single variable "Ascending". 注意单个变量“ Ascending”的设置。

The middle or child component has: 中间或子组件具有:

  export const TableHeaders = (props) => {
  const { onSortDirectionChange } = props;
  return (
    <GlyphiconDirectionChange onSortDirectionChange={onSortDirectionChange} />
  )

The top or container component has: 顶部或容器组件具有:

  class ClientsContainer extends Component {

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

  return (
    <div className="row">

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

    </div>
  )
 }

My question is , without worry in this instance about the context eg why would you need two or more variables to manage sort direction.. how would you pass more than one parameter from the bottom component and make sure that it is applied to the correct variable in "HandleSortDirection"... How would set a 2nd and or 3rd parameter be set in "GlyphiconDirectionChange" and then how would you retrieve it in "HandleSortDirection" (whats the correct syntax)? 我的问题是 ,在这种情况下不必担心上下文,例如,为什么需要两个或多个变量来管理排序方向。如何从底部组件传递多个参数并确保将其应用于正确的变量在“ HandleSortDirection”中...如何在“ GlyphiconDirectionChange”中设置第二个或第三个参数,然后如何在“ HandleSortDirection”中检索它(正确的语法)?

try to pass an object as the first param in your 'bottom component'. 尝试将一个对象作为“底部组件”中的第一个参数传递。

const GlyphiconDirectionChange = (props) => {
  const { onSortDirectionChange } = props
  let options = {
    order: 'Ascending',
    // ... and other props you want
    offset: 10
  }
  return (
    <span
      onClick={() => onSortDirectionChange(options)}
      className='glyphicon glyphicon-sort-by-attributes' />
  )
 }

so in the 'top component', you'll get 因此,在“顶部组件”中,您将获得

handleSortDirection = (options = {}) => {
  const { sortDirection, query, sortBy  } = this.props
  let { order, offset } = options
  ...
}

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

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