猿问

redux中通过props传递action的疑问?

最近在用react + redux 做开发,遇到这样一个问题。
我从后台获取这样一组数据

[

    {

        type: 'button',

        ...

    },

    {

        type: 'text',

        ...

    }

]

然后通过统一的方法把数据渲染成自定义的Button和Text控件。
例如:

let component = components[type];return React.createElement(component, {action1: action1, action2: action2});

现在的问题是:我有一些action是Button控件独有的,一些action是Text控件独有的,我在创建控件的时候要怎么设计才可以给不同的控件传递不同的action而不是把所有的action都传递给它们?


白猪掌柜的
浏览 840回答 1
1回答

幕布斯6054654

既然都用 type 去取专门的组建了,就区分 type 传特定的 Action 呗。let component = components[type];let componentActions = actions[type];return React.createElement(component, componentActions);
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答