Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';不要在组件内部使用redux操作_Javascript_Reactjs_Redux_Single Page Application - Fatal编程技术网

Javascript Can';不要在组件内部使用redux操作

Javascript Can';不要在组件内部使用redux操作,javascript,reactjs,redux,single-page-application,Javascript,Reactjs,Redux,Single Page Application,加载时我有一个组件我想分派一个动作,我使用了mapDispatchToProps,但我无法在props中访问该动作,但是它在其他组件中工作,但每当我从componentDidMount调用此函数时,它都会给我类型错误:没有这样的函数,我选中了。。。我的代码中没有输入错误,即使这样,我似乎也无法让它正常工作 import React, { Component } from "react"; import { connect } from "react-redux"; import { getUse

加载时我有一个组件我想分派一个动作,我使用了mapDispatchToProps,但我无法在props中访问该动作,但是它在其他组件中工作,但每当我从componentDidMount调用此函数时,它都会给我类型错误:没有这样的函数,我选中了。。。我的代码中没有输入错误,即使这样,我似乎也无法让它正常工作

import React, { Component } from "react";
import { connect } from "react-redux";
import { getUserDetails } from "../../store/actions/usersActions";

export class UserDetails extends Component {
  state = {
    user: this.props.userDetails
  };

  componentDidUpdate(prevProps, prevState, snapshot) {
    if (prevProps.userDetails !== this.props.userDetails) {
      this.setState({
        user: this.props.userDetails
      });
    }
  }

  componentDidMount() {
    console.log(this.props);
    //this.props.getUserDetails(this.props.match.params.username);
  }

  render() {
    const { user } = this.state;

    return user ? (
      <div className="userDetails">
        <p>{user.username}</p>
      </div>
    ) : (
      <div className="userDetails" />
    );
  }
}

const mapStateToProps = state => {
  return {
    userDetails: state.user.userDetails
  };
};

const mapDispatchToProps = dispatch => {
  return {
    getUserDetails: username => dispatch(getUserDetails(username))
  };
};

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

componentDidMount() {
  19 |    console.log(this.props);
> 20 |    this.props.getUserDetails(this.props.match.params.username);
     | ^  21 |  }
  22 | 
  23 |  render() {
View compiled
commitLifeCycles
D:/Web Projects/min-social-media/client/node_modules/react-dom/cjs/react-dom.development.js:18109
  18106 |       !(instance.state === finishedWork.memoizedState) ? warning$1(false, 'Expected %s state to match memoized state before ' + 'componentDidMount. ' + 'This might either be because of a bug in React, or because ' + 'a component reassigns its own `this.props`. ' + 'Please file an issue.', getComponentName(finishedWork.type) || 'instance') : void 0;
  18107 |     }
  18108 |   }
> 18109 |   instance.componentDidMount();
        | ^  18110 |   stopPhaseTimer();
  18111 | } else {
  18112 |   var prevProps = finishedWork.elementType === finishedWork.type ? current$$1.memoizedProps : resolveDefaultProps(finishedWork.type, current$$1.memoizedProps);

import React,{Component}来自“React”;
从“react redux”导入{connect};
从“./../store/actions/usersActions”导入{getUserDetails};
导出类UserDetails扩展组件{
状态={
用户:this.props.userDetails
};
componentDidUpdate(prevProps、prevState、快照){
if(prevProps.userDetails!==this.props.userDetails){
这是我的国家({
用户:this.props.userDetails
});
}
}
componentDidMount(){
console.log(this.props);
//this.props.getUserDetails(this.props.match.params.username);
}
render(){
const{user}=this.state;
返回用户(
{user.username}

) : ( ); } } 常量mapStateToProps=状态=>{ 返回{ userDetails:state.user.userDetails }; }; const mapDispatchToProps=调度=>{ 返回{ getUserDetails:username=>dispatch(getUserDetails(username)) }; }; 导出默认连接( MapStateTops, mapDispatchToProps )(用户详细信息); componentDidMount(){ 19 | console.log(this.props); >20 | this.props.getUserDetails(this.props.match.params.username); | ^ 21 | } 22 | 23 | render(){ 视图编译 承诺周期 D:/Web Projects/min社交媒体/client/node_modules/react dom/cjs/react dom.development.js:18109 18106 |!(instance.state==finishedWork.MemorizedState)?警告$1(false,“预期%s状态与“+”componentDidMount之前的Memorized状态匹配。“+”这可能是因为React中存在错误,或者是因为“+”组件重新分配了自己的“This.props”。“+”请提交问题。“,getComponentName(finishedWork.type)|“instance”):void 0; 18107 | } 18108 | } >18109 | instance.componentDidMount(); |^18110 | stopPhaseTimer(); 18111 |}其他{ 18112 | var prevProps=finishedWork.elementType==finishedWork.type?当前$$1.memoizedProps:resolveDefaultProps(finishedWork.type,当前$$1.memoizedProps);
您编写了
dispath
,看起来应该是
dispatch
不,似乎仍然不起作用hmm您想制作一个codesandbox吗?如果我们看到您的其余代码,我们可能可以帮您解决它。根据您发布的内容,似乎没有任何问题。情况可能不是这样,但可以检查>>您正在导出c组件本身导出类UserDetails以及react-redux连接组件作为默认导出导出默认连接(MapStateTops,mapDispatchToProps)(UserDetails)导入此组件时,请确保导入的是连接的组件,它是默认导出您是对的,我是这样导入的{UserDetails},谢谢你的帮助!你写了
dispath
,看起来应该是
dispatch
不,似乎仍然不起作用。你想制作一个codesandbox吗?如果我们看到你的其余代码,我们可能可以帮你解决它。根据你发布的内容,似乎没有任何问题。情况可能不是这样,但可以检查>>你的答案e导出组件本身导出类UserDetails,并将react-redux连接组件作为默认导出导出默认连接(mapStateToProps,mapDispatchToProps)(UserDetails)当您导入此组件时,请确保您正在导入连接的组件,这是默认的导出。您是对的,我像{UserDetails}一样导入它,谢谢您的帮助!