【问题标题】:Initialize component with Async data使用异步数据初始化组件
【发布时间】:2016-09-17 06:43:39
【问题描述】:

我正在尝试弄清楚 如何以及在何处 为我在 react + redux + thunk 中的选择框加载数据(即在我的操作上调用调度)。我不确定它是否应该进入我的 App 容器的构造函数中,或者我应该将它加载到我的组件中(在我的示例中:“MyDropdown”)

我的主应用:

import MyDropdown from '../components/mydropdown';
// Should i import my action here and then...
// import { loadData } from '../actions';

class App extends Component {
  render() {
    return (
      <div className="page-content">
        <div className="option-bar">
          // SEND it as a PROP inside MyDropdown... 
          <MyDropdown />
        </div>
      </div>
    );
  }
}
export default App;

我的组件

// OR.. Should i load it in my MyDropdown component here?
import { loadData } from '../actions';

class MyDropdown extends Component {
  // If i load it here on load, how do i do it?
  render() {
    return(
      <select>
         {renderOptions()}
      </select>
    );
  }
}

我在我的 App 类中尝试了 componentDidMount(),但它似乎没有用。将初始化数据和调用动作放在那里似乎是有意义的,因为它将全部集中,而不是在我的子组件中调用动作。另外,我将有多个需要在启动时加载的选择框,所以我的 App 类可能会增长很多,这是正确的方法吗?我不确定最佳做法是什么,因为我才刚刚开始学习 react。

【问题讨论】:

    标签: reactjs redux react-redux redux-thunk


    【解决方案1】:

    您应该将数据组件与表示组件分开(参见帖子here)。

    所以在你的小例子中,MyDropdown 应该传递它需要的所有数据以呈现组件。这意味着在 App(或实际呈现视图的组件的某些父组件)中获取数据。

    由于您使用的是 React 和 Redux,react-redux 库提供了一个帮助函数来生成容器,这些容器可以获取您的演示组件所需的数据。

    为此,请将App 更改为:

    import { connect } from 'react-redux'
    import MyDropdown from '../components/mydropdown';
    import { loadData } from '../actions';
    
    // This class is not exported
    class App extends Component {
      componentDidMount() {
        this.props.loadData()
      }
      render() {
        return (
          <div className="page-content">
            <div className="option-bar">
              <MyDropdown data={this.props.data}/>
            </div>
          </div>
        );
      }
    }
    
    function mapStateToProps(state) {
      const { data } = state
      return {
        data
      }
    }
    
    function mapDispatchToProps(dispatch) {
      return {
        loadData(){
          dispatch(loadData())
        }
      }
    }
    
    // Export a container that wraps App
    export default connect(mapStateToProps, mapDispatchToProps)(App);
    

    或者,您可以保持 App 不变并将 MyDropdown 更改为:

    import { connect } from 'react-redux'
    import { loadData } from '../actions';
    
    // Exporting this allows using only the presentational component
    export class MyDropdown extends Component {
      componentDidMount() {
        this.props.loadData()
      }
      render() {
        return(
          <select>
             {renderOptions(this.props.data)}
          </select>
        );
      }
    }
    
    function mapStateToProps(state) {
      const { data } = state
      return {
        data
      }
    }
    
    function mapDispatchToProps(dispatch) {
      return {
        loadData(){
          dispatch(loadData())
        }
      }
    }
    
    // By default, export the container that wraps the presentational component
    export default connect(mapStateToProps, mapDispatchToProps)(MyDropdown);
    

    在这两种情况下,查看最后实际导出的默认值。这不是组件;这是connect 的回归。该函数包装您的展示组件并返回一个容器,该容器负责获取数据并为展示组件调用操作。

    这为您提供了所需的分离,并允许您灵活地使用演示组件。在任一示例中,如果您已经拥有需要渲染 MyDropdown 的数据,则可以只使用演示组件并跳过数据获取!

    您可以在 Redux 文档 here 中查看完整示例。

    【讨论】:

      猜你喜欢
      • 2013-04-23
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-04
      • 2021-01-24
      相关资源
      最近更新 更多