redux - inject one user data to UserHeader

Current component get users collection from props.
UserHeader component seems weird because the component is designed to show only one user, should not get all user data and find one of it,

In order to inject only one User data to UserHeader, can change implementation in mapStateToProps

Commit

Code
import React, {Component} from "react";
import {connect} from 'react-redux'
import {fetchUser} from "../actions";

class UserHeader extends Component {

    componentDidMount() {
        this.props.fetchUser(this.props.userId)
    }

    render() {
        return <div>Author: {this.props.user?.name}</div>
    }
}

const mapStateToProps = (state, props) => {
    return {user: state.users?.find(user => user.id === props.userId)}
}

export default connect(mapStateToProps, {fetchUser})(UserHeader)

沒有留言:

張貼留言

別名演算法 Alias Method

 題目 每個伺服器支援不同的 TPM (transaction per minute) 當 request 來的時候, 系統需要馬上根據 TPM 的能力隨機找到一個適合的 server. 雖然稱為 "隨機", 但還是需要有 TPM 作為權重. 解法 別名演算法...