将事件绑定到组件-React,Redux

时间:2019-01-13 16:28:51

标签: javascript reactjs ecmascript-6 react-redux

我有一个像这样的组件:

int a = 0; int* d = &a; int x; x = **(&d);

我正在将此应用程序导入应用程序的根目录-class GlobalComponent extends React.Component{ select= (e) => { this.props.select(); // must be implemented in another component and not parent }; render(){ return( <div> {this.state.data} <Button onClick={this.select}>Select</Button> </div> ); } } function mapStateToProps(state) { return state.data } const mapDispatchToProps = (dispatch, props) => ({ select: () => dispatch(select()), }); export default connect(mapStateToProps, mapDispatchToProps)(GlobalComponent ); 中,

app.js

问题是此操作<GlobalComponent /> 。我知道我可以使用道具从全局状态或父组件传递它(例如):

select

或带有<GlobalComponent select={this.select} />

,但是问题在于该组件仅在一个位置定义,否则我需要在几乎所有组件中导入mapDispatchToProps。 我想要的是一次(在根中的某个地方)导入组件,然后以某种方式在使用GlobalComponent的组件中定义一个函数,该函数将不会执行GlobalComponent,但会实现select并且仅用于GlobalComponent中的click事件:

select

这是可能的,这是一种好方法还是只是将组件导入到任何地方都更好?

我正在使用和export default class ComponentThatUsesGlobal extends React.Component { executeWhenGlobalComponentBtnIsClicked = () => { } }

0 个答案:

没有答案