简体   繁体   English

如何从React Redux中的子组件发送?

[英]How can I dispatch from child components in React Redux?

My server has code like this: 我的服务器有这样的代码:

<ReactRedux.Provider store={store}><Layout defaultStore={JSON.stringify(store.getState())}/></ReactRedux.Provider>

<Layout> obviously has more components which nest more. <Layout>显然有更多的组件嵌套更多。

I have a class like this deeper down: 我有一个这样的课程更深层次:

import React from 'react';

export default React.createClass({
  render: function(){
    var classes = [
      'js-select-product',
      'pseudo-link'
    ];

    if (this.props.selected) {
      classes.push('bold');
    }

    return (
      <li className="js-product-selection">
        <span onClick={this.props.onClick} className={classes.join(' ')} data-product={this.props.id}>{this.props.name}</span>
      </li>
    );
  }
});

What I really want to do rather than this.props.onClick is dispatch an event to set state in a reducer . 我真正想要做的而不是this.props.onClick是一个事件,用于在reducer中设置状态 I've been some things online about context but I've gotten mixed reviews as that feature was or wasn't going away. 我已经在网上关于上下文的一些事情,但我得到了不同的评论,因为该功能已经或不会消失。

EDIT I see this connect method but I could have sworn I'd read not to use connect in children components. 编辑我看到这个连接方法,但我可以发誓我读过不要在子组件中使用connect

You're getting too caught up on children components. 您对儿童组件的关注太多了。 You should structure your app so that you have connected components and non-connected components. 您应该构建应用程序,以便连接组件和非连接组件。 Non-connected components should be stateless, pure functions essentially, that take in all their requirements via props. 非连接组件应该是无状态的,纯粹的功能,通过道具来满足他们的所有要求。 Connected components should use the connect function to map redux state to props and redux dispatcher to props, and then be responsible for passing those props to child components. 连接组件应该使用connect函数将redux状态映射到props,将redux调度程序映射到props,然后负责将这些props传递给子组件。

You might have lots of connected components in an app, and lots of non-connected components. 您可能在应用程序中有许多连接的组件,以及许多未连接的组件。 This post (by the creator of redux) discusses it in more detail, and talks about non-connected (dumb) components being responsible for actual display of UI, and connected (smart) components being responsible for composing non-connected components. 这篇文章 (由redux的创建者)更详细地讨论了它,并讨论了负责实际显示UI的非连接(哑)组件,以及负责组成非连接组件的连接(智能)组件。

An example might be (using some newer syntax): 一个例子可能是(使用一些较新的语法):

class Image extends React {
  render() {
    return (
      <div>
        <h1>{this.props.name}</h1>
        <img src={this.props.src} />
        <button onClick={this.props.onClick}>Click me</button>
      </div>
    );
  }
}

class ImageList extends React {
  render() {
    return (
      this.props.images.map(i => <Image name={i.name} src={i.src} onClick={this.props.updateImage} />)
    );
  }
}

const mapStateToProps = (state) => {
  return {
    images: state.images,
  };
};
const mapDispatchToProps = (dispatch) => {
  return {
    updateImage: () => dispatch(updateImageAction()),
  };
};
export default connect(mapStateToProps, mapDispatchToProps)(ImageList);

In this example, ImageList is a connected component and Image is a non-connected component. 在此示例中, ImageList是连接组件, Image是非连接组件。

There used to be advice to the effect to try to limit the components that you connect. 曾经有过建议,试图限制你连接的组件。 See for example: 参见例如:

https://github.com/reactjs/redux/issues/419 https://github.com/reactjs/redux/issues/419

https://github.com/reactjs/redux/issues/419#issuecomment-178850728 https://github.com/reactjs/redux/issues/419#issuecomment-178850728

Anyway, that's really more useful for delegating a slice of state to a component. 无论如何,这对于将一片状态委托给一个组件来说真的更有用。 You can do that if it makes sense for your situation, or if you don't want to pass down a callback that calls dispatch() you can pass the store or dispatch down the hierarchy if you want. 如果它对您的情况有意义,或者如果您不想传递调用dispatch()的回调,则可以执行此操作,如果需要,可以传递存储或按层次结构调度。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM