【问题标题】:Redux mapDispatchToProps access action within array.maparray.map 中的 Redux mapDispatchToProps 访问操作
【发布时间】:2019-10-09 20:27:46
【问题描述】:

我正在尝试在 array.map 中调用的函数中获取可用的操作。正在将道具从父组件传递到子组件。感觉在 array.map 函数中使其可用应该是一项简单的任务,但事实证明这并不成功。

这是一个更新的示例代码:

在这个例子中,能够正确地将 'actionUpdate' 函数传递给 listItem 是我没有成功完成的。

function listItem(item,index) {
    const id = item.id

    const handleUpdated = e => {
        e.preventDefault();
        actionUpdate(id);
    };
    return (
        <Button
            onClick={handleUpdated}
            color='primary'
            variant='contained'
            style={{ marginTop: '0.75rem' }}
        >
            Update
        </Button>

    );
}
function MyList(props) {
    const { streams } = props;

    return (
        <List>
            {streams.map(listItem)};
        </List>
    );
}

function listView(props) {
    const { classes, streams } = props;

    return (
        <div style={{ width: '100%' }}>
            <section className={classes.content}>
                <Grid container>
                    <Grid item style={{ width: '100%' }}>
                        <MyList
                            streams={streams}
                            actionUpdate={actionUpdate}
                        />
                    </Grid>
                </Grid>
            </section>
        </div>
    );
}

const mapStateToProps = state => {
    const streams = R.path(['theObject', 'arrayOfObjects'])
    )(state);
    return { streams };
};

const mapDispatchToProps = dispatch => {
    const actionUpdate = (itemId) => {
        return dispatch(theUpdateAction(itemId));
    };
    return { actionUpdate };
};

const MainListView = connect(
    mapStateToProps,
    mapDispatchToProps
)(listView);

export default withStyles(styles)(MainListView);

我使用 connect、mapStateToProps 和 mapDispatchToProps 将我的状态和操作映射到道具。

我需要实现的是从 listItem 函数中的调度访问操作。

【问题讨论】:

  • 如果没有最小的可重复示例,很难看到你在做什么。我想说尝试通过props.listItem访问listItem
  • @MattOestreich 我添加了一个更完整的示例
  • 您可以使用 CodeSandbox 或 StackBlitz 之类的工具来创建可重现的示例吗?很难看到你正在尝试做什么。帮我帮你。

标签: javascript reactjs redux


【解决方案1】:

您可以在 MyList 中定义 listItem,这将使其能够访问 MyList 的道具,包括 updateAction。

function MyList(props) {
    const { streams, actionUpdate } = props;

    // Can access updateAction here
    const listItem = (item,index) => {
        const id = item.id

        const handleUpdated = e => {
            e.preventDefault();
            actionUpdate(id);
        };
        return (
            <Button
                onClick={handleUpdated}
                color='primary'
                variant='contained'
               style={{ marginTop: '0.75rem' }}
            >
                Update
            </Button>
        );
       }

    return (
        <List>
            {streams.map(listItem)};
        </List>
    );
}

function listView(props) {
    const { classes, streams } = props;

    return (
        <div style={{ width: '100%' }}>
            <section className={classes.content}>
                <Grid container>
                    <Grid item style={{ width: '100%' }}>
                        <MyList
                            streams={streams}
                            actionUpdate={actionUpdate}
                        />
                    </Grid>
                </Grid>
            </section>
        </div>
    );
}

const mapStateToProps = state => {
    const streams = R.path(['theObject', 'arrayOfObjects'])
    )(state);
    return { streams };
};

const mapDispatchToProps = dispatch => {
    const actionUpdate = (itemId) => {
        return dispatch(theUpdateAction(itemId));
    };
    return { actionUpdate };
};

const MainListView = connect(
    mapStateToProps,
    mapDispatchToProps
)(listView);

export default withStyles(styles)(MainListView);

【讨论】:

  • 这是迄今为止最直接的答案并且有效。谢谢你。我希望将它们分开,因为它会构成一个非常大的 MyList 组件,但最好是工作。
猜你喜欢
  • 2019-02-11
  • 2017-07-04
  • 2019-05-27
  • 1970-01-01
  • 2019-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多