简体   繁体   English

如何将数据从 mapStateToProps 传递到父组件?

[英]How to pass data from mapStateToProps to parent component?

This is my component:这是我的组件:

export default const NotesComponent = () => {
  return notesList.map((noteProps) => (
    <NewsCard {...notesProps} key={notesProps.id} />
  ))
}

Here is the place where i use this component:这是我使用这个组件的地方:

const Notes = (props: NotesProps) => {
  return (
    <Container>
      <NotesComponent />
    </Container>
  )
}

const mapStateToProps = (state) => ({
  notesData: state.notes.notesData,
})
// and other code so mapStateToProps is working correctly

I don't know how to pass notesData to NotesComponent , so the NewsCard can read the data.我不知道如何将notesData传递给NotesComponent ,因此NewsCard可以读取数据。

You can use connect high-order-function from react-redux and export the returned component:您可以使用 react-redux 中的连接高阶函数并导出返回的组件:

import { connect } from 'react-redux'

// Redux state data notesData will be available in props
const NotesComponent = ({notesData}) => { 
  return notesData.map((noteProps) => (
    <NewsCard {...noteProps} key={noteProps.id} />
  ))
}

const mapStateToProps = (state) => ({
  notesData: state.notes.notesData,
})

export default connect(mapStateToProps)(NotesComponent)

Or, as NotesComponent is a function component, you can use react-hook useSelector instead of using connect to read redux data:或者,由于NotesComponent是 function 组件,您可以使用 react-hook useSelector而不是使用connect来读取 redux 数据:

// in a function component
import { useSelector } from 'react-redux'
...
const notesData = useSelector((state) => state.notes.notesData)

Edit:编辑:

You can also connect parent component ie Notes with Redux and pass data to NotesComponent in props (to make NotesComponent a dumb or reusable component):您还可以将父组件(即Notes )与 Redux 连接,并在 props 中将数据传递给NotesComponent (使NotesComponent成为哑组件或可重用组件):

interface NotesProps {
  notesData: write_your_type[]
  // ...
}

const Notes = (props: NotesProps) => {
  const { notesData } = props
  return (
    <Container>
      <NotesComponent data={notesData} />
    </Container>
  )
}

const mapStateToProps = (state) => ({
  notesData: state.notes.notesData,
})

export default connect(mapStateToProps)(Notes) 
// it now exports enhanced (with redux data in props) Notes component

And, NotesComponent :并且, NotesComponent

export default const NotesComponent = ({data}) => { 
  return data.map((item) => (
    <NewsCard {...item} key={item.id} />
  ))
}

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

相关问题 如何在组件从Firebase接收数据后映射StateToProps - How to mapStateToProps only after the component receives data from Firebase 如何将数据从子组件传递到reactjs中的父组件 - How to pass data from child component to parent component in reactjs 如何将表单数据从子组件传递到父组件 - How to pass form data from child component to parent component 如何在 React 功能组件中将数据从孙子传递到父组件? - How to pass data from Grandchild to Parent component in React Functional Component? 就我而言,如何将数据从子组件传递到父组件? - In my case, how to pass data from child component to parent component? 如何在 React 中将数据从一个组件传递到另一个组件/父级 - How to pass data from one component to another component/parent in React 如何将数据从子组件传递到父组件? - How to pass data from child component to parent component? 如何将数据从子组件传递到父组件 - how to pass the data from child component to parent component 如何将大量数据从父组件传递到子组件 - How to pass a large list of data from parent to child component 如何在 React 中将数据从映射对象传递到其父组件? - How to pass data from mapped objects to its parent component in React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM