【问题标题】:Redux provider not provides the props to React Component on loadRedux 提供程序未在加载时为 React 组件提供道具
【发布时间】:2020-02-02 07:17:49
【问题描述】:

我正在使用 Redux 进行状态管理。 我收到道具未定义的错误。 下面的块显示了我的代码,

index.js

ReactDOM.render(
      <Provider store={configureStore()}>
        <App />
      </Provider>,
      document.getElementById('root')
  );

store.js

function configureStore(state = { rotating: true }) {
  return createStore(rotateReducer,state);
}

浏览器控制台:

App {props: undefined, context: undefined, refs: {…}, updater: {…}}
on expand this App object in console, 
props: {rotating: true, startAction: ƒ, stopAction: ƒ} (now props are defined).

App.js

function App() {
    return (
      <div className="App">
        <header className="App-header">
        <img 
            src={logo} 
            className={
              "App-logo" + 
              (this.props.rotating ? "":" App-logo-paused")
            } 
            alt="logo" 
            onClick={
              this.props.rotating ? 
                this.props.stopAction : this.props.startAction
            }
          />
          <p>
            Edit <code>src/App.js</code> and save to reload.
          </p>
          <a
            className="App-link"
            href="https://reactjs.org"
            target="_blank"
            rel="noopener noreferrer"
          >
            Learn React
          </a>
        </header>
      </div>
    );
  }

export default connect(mapStateToProps, mapDispatchToProps)(App);

App 组件加载的 props 有什么问题?

【问题讨论】:

  • 能否也提供App 组件代码。
  • 您在App 组件中定义mapStateToProps 的位置?
  • App 函数上方... const mapStateToProps = state => ({ ...state }); const mapDispatchToProps = dispatch => ({ startAction: () => dispatch(startAction), stopAction: () => dispatch(stopAction) });

标签: reactjs redux react-redux


【解决方案1】:

去掉props前的this关键字,它是一个函数组件,this只在class组件中使用。

并在函数签名中添加props参数。

试试这个

示例:

import React from "react";
import { connect } from "react-redux";

function App(props) {
  return (
    <div className="App">
      <header className="App-header">
        <img
          src={logo}
          className={
            "App-logo" + (props.rotating ? "" : " App-logo-paused")
          }
          alt="logo"
          onClick={
            this.props.rotating ? props.stopAction : props.startAction
          }
        />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

const mapStateToProps = state => ({ ...state });
const mapDispatchToProps = dispatch => ({
  startAction: () => dispatch(startAction),
  stopAction: () => dispatch(stopAction)
});

export default connect(mapStateToProps, mapDispatchToProps)(App);

【讨论】:

  • 是的,我在我的代码中也是这样。浏览器抛出的道具未定义。似乎提供者没有正确地将道具传递给 App 功能/组件。
  • 您必须将props 作为函数参数传递
  • 知道了。由于组件定义为函数,我们需要将道具传递给函数参数。否则我们必须更改为 Class。谢谢。
猜你喜欢
  • 2018-11-12
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 2021-08-20
  • 2017-11-21
  • 1970-01-01
相关资源
最近更新 更多