简体   繁体   English

使用 react-redux 我正在尝试创建中间件来执行异步等待逻辑,以便能够使用 this.props.getUser 操作 function

[英]Using react-redux I'm trying to create middleware to do async await logic to be able to use this.props.getUser action function

> // I get user here with action function which makes call to route

/api/users/$_id
    export const getUser = (_id) => (dispatch) => {
        dispatch(setUsersLoading());
        axios.get(`/api/users/${_id}`)
            .then(res =>
                dispatch({
                    type: GET_USER,
                    payload: res.data
                }))
            .catch(err =>
                dispatch(returnErrors(err.response.data, err.response.status))
            );
    }
    

> // This is my Manage Role Assignment component class

    class ManageRoleAssignment extends Component {
        constructor(props) {
            super(props)
    
            this.state = {
                currentPage: 0,
                roles: "",
                selected: [], // this the state I'm using!
                personnel: []
            }
        }
    
        static propTypes = {
            getUsers: PropTypes.func.isRequired,
            **getUser: PropTypes.func.isRequired**, //Prop action function used!
            users: PropTypes.object.isRequired,
            **user: PropTypes.object.isRequired**, //The prop object used!
        }
        componentDidMount() {
            this.props.getUsers(); 
    
        }
    
        handleChange = (e) => {
        let target = e.target
        let name = target.name
        let value = Array.from(target.selectedOptions, option => option.value)
        this.setState({
            [name]: value
        })

       
    }

> I'm calling the this.props.getUser here in the onSubmit

        onSubmit = (e) => { 
        console.log("SELECTED", this.state.selected)
        console.log("PERSONNEL", this.state.personnel)
        //let { user } = this.props.user
        this.props.getUser(this.state.selected)

        console.log("USER", this.props.user)
        e.preventDefault();
        }

The beginning of my render function for the class我的开始渲染 function 为 class

        render() {
        let { users } = this.props.users
        const { currentPage } = this.state
        this.pageSize = 1;
        this.pagesCount = Math.ceil(10 / this.pageSize)
    
        return (
            <div>
                <Container className="list">
                    <form onSubmit={this.handleSubmit}>
                        <ListGroup>

This is the beginning of my multi select element where the users are selected for the this.state.selected state这是我的多 select 元素的开始,其中为 this.state 选择了用户。选择 state

                            <h5>Select 1 or more users.</h5>
                            <select size="2" value={this.state.selected} onChange={this.handleChange}
                                style={{ width: 265 + 'px', fontSize: 20 + 'px' }} name="selected" id="users" multiple>
                                {users.map(({ _id, name, email, role }) => (
                                    <option value={_id}>
                                        {name}
                                    </option>
                                ))}
                            </select>
                        </ListGroup>
                        <br />
                        <br />

This is where I'm going to be selecting the role for the users, it just list roles这是我要为用户选择角色的地方,它只是列出角色

                        <h5>Select a role to assign.</h5>
                        <select value={this.state.personnel} onChange={this.handleChange}
                            style={{ width: 265 + 'px', fontSize: 20 + 'px' }} name="personnel">
                            {Object.keys(Role).map(r => <option value={r}>{Role[r]}</option>)}
                        </select>
                        <br />
                        <Button style={{ marginLeft: 100 + 'px' }}>Submit </Button>
                    </form>
                    <Container className="personnelTable">
                        <Table size="sm" striped >
                            <thead>
                                <tr >
                                    <th><h5>Name</h5></th><th><h5>Email</h5></th><th><h5>Role</h5></th>

                                </tr>
                            </thead>

This is where I'm listing the value for testing purpose which is the _id of the users that were selected from the multi selelect element above这是我列出用于测试目的的值的地方,它是从上面的多选择元素中选择的用户的 _id

                            {this.state.selected.map(value => (
                                <tr>
                                    {value}
                                </tr>
                            ))}
                        </Table>
                        <TablePagination
                            currentPage={currentPage}
                            pagesCount={this.pagesCount}
                            handlePageClick={this.handlePageClick}
                        />
                    </Container>
                </Container>

            </div >
        )
    }

}
const mapStateToProps = (state) => ({
    users: state.users,
    user: state.users
})

export default connect(mapStateToProps, { getUsers, getUser })((ManageRoleAssignment))

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

相关问题 react-redux 中的异步请求使用 async/await - Async request in react-redux using async/await 在firebase中使用异步/等待 react-redux | 反应性的 - Using Async/Await in firebase | react-redux | react-native React-Redux - 解析错误:不能在异步 function 之外使用关键字“等待” - React-Redux - Parsing error: Can not use keyword 'await' outside an async function react-redux动作创建者在道具上不可用 - react-redux action creator not available on props react-redux:为什么我得到_this.props.incrementCounter不是函数? - react-redux: why do I get _this.props.incrementCounter is not a function? This.props.dispatch 不是函数 - React-Redux - This.props.dispatch not a function - React-Redux React-Redux:this.props.AppendCharacter不是函数 - React-Redux: this.props.AppendCharacter is not a function 错误:动作必须是普通对象。 使用自定义中间件进行异步操作。 React-redux错误 - Error: Actions must be plain objects. Use custom middleware for async actions. React-redux error React-Redux:动作必须是普通对象。 使用自定义中间件进行异步操作错误 - React-Redux: Actions must be plain objects. Use custom middleware for async actions Error react-redux 错误:操作必须是普通对象。 使用自定义中间件进行异步操作 - react-redux Error: Actions must be plain objects. Use custom middleware for async actions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM