简体   繁体   English

从redux传递的调用方法在从子级到父级的反应中

[英]Call method passed from redux in react from child to parent

i'm stucked with calling method passed from my main root component to my very nested component. 我陷入了从我的主根组件传递到非常嵌套的组件的调用方法的困境。 I am passing that function from redux, and somehow I am having problem and i can't execute it... 我正在从redux传递该函数,并且以某种方式我遇到了问题,我无法执行它...

postActions.js postActions.js

export function deletePost(id) {
        const request = axios.delete(`http://localhost:3000/api/posts/${id}`);
        return {
        type: "DELETE_POST",
        payload: request
    }
}

App.js(main root component) App.js(主要根组件)

import {deletePost} from "../actions/postActions";
const mapStateToProps = (state) => {
    return {
        post: state.postReducer
    };
};

const mapDispatchToProps = (dispatch) => {
    return {
        deletePost:(id)=>{
            dispatch(deletePost(id));
        }
    }
}

Also in my App component(root) i have render method with this PropsRoute which is like normal route but allows me to pass props... 同样在我的App组件(root)中,我具有与此PropsRoute的render方法,这类似于普通路线,但允许我传递道具...

<PropsRoute path={"/posts/:id/:title"}  deletePost={this.props.deletePost} component={Posts} {...this.props.match} />  

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

Posts.js Posts.js

render(){    
    return(
        <div> 
            <main>   
                <Post deletePost={this.props.deletePost)} />
            </main>
        </div>
    ); 
    }

Post.js(here i should to execute it) Post.js(在这里我应该执行它)

render(){
   return (
     <LabelDelete handleDelete={this.deletePost} id={id}  {...this.props.children}/>
   )
}

And finally const LabelDelete 最后是const LabelDelete

const LabelDelete = (props) => (<span><button onClick={props.handleDelete}>Delete</button></span>);

So something here wont work, and i am getting error TypeError: _this2.deletePost is not a function at handleDelete and i dont know where to bind this ... 所以这里有些东西行不通,并且我收到错误TypeError:_this2.deletePost不是handleDelete的函数,我不知道在哪里绑定 ...

But it worked since i didn't use this way with redux it was 但是它起作用了,因为我没有在redux中使用这种方式

<LabelDelete handleDelete={this.handleDelete.bind(this)} id={id}  {...this.props.children}/>

And handleDelete function looked like this: handleDelete函数如下所示:

handleDelete(){
    var id = this.props.id; 
    $.ajax({ 
        type:"DELETE",
        url:`http://localhost:3000/api/posts/${id}`, 
        success: function(res) { 
            setTimeout(function () {
              location.pathname="/user";
            }, 500);
        }.bind(this),
        error: function(xhr, status, err) {
            console.error(xhr, status, err.toString());
        }.bind(this)
      }); 
  }   

This worked, but i don't want to use it like this, i want it to be more clear. 这行得通,但我不想这样使用它,我希望它更清晰。 Any help? 有什么帮助吗? Thanks 谢谢

In your Posts.js, you are passing deletePost as props of Post component. 在Posts.js中,您将deletePost作为Post组件的支持传递。 Thus, in Post.js, this.deletePost should be changed to this.props.deletePost. 因此,在Post.js中,this.deletePost应该更改为this.props.deletePost。

render(){
    return (
        <LabelDelete handleDelete={this.props.deletePost} id={id}  
            {...this.props.children}/>
    )
}

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

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