【问题标题】:Adding a new object or collection of objects to props将新对象或对象集合添加到道具
【发布时间】:2018-05-24 20:14:24
【问题描述】:

我有一个 React 页面,它从使用 saga 中间件的 redux 商店加载了一堆东西。

在这个特定页面中,我想加载一些不是通过 props 传入的新内容。

因此,我为上传的个人资料照片的新集合创建了一个新的 reducer、action 和 watcher。

有没有办法加载这个我设置的新集合而不将它作为道具传递?

谢谢! :)

这是我对该页面的反应功能:

export default function profileEdit(props) {
    //passed in as props
    const { stats, weapons, armor, readOnly } = props;

    //not passed in as props
    const { profilePics } = ??;

    return (
        <div></div>
    )

【问题讨论】:

  • 也许解释一下您为什么要避免使用道具会让我们更好地帮助您。

标签: reactjs react-redux redux-saga


【解决方案1】:

你可以使用 props 或 context api 来传递一些东西给 react 组件。

由于您的图片在 redux 存储中,因此正确的方法是将您的组件包装在来自 react-reduxconnect 函数包装器中,并通过 mapStateToProps 选择集合。

这样,您来自 redux 存储的数据将被注入到组件的 props 中,以便您可以将它们显示出来。

这里是一个例子:https://gist.github.com/markerikson/121c77a01c453466361a9c6434a08620

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    相关资源
    最近更新 更多