【问题标题】:Ticket Details are not being displayed (but show up in redux state)工单详细信息未显示(但显示为 redux 状态)
【发布时间】:2018-07-18 19:03:14
【问题描述】:

我有一个 TicketList,它映射了数据库中的所有票。 ID、价格和描述显示在列表中。 ID是可点击的 并且应该将您重定向到向您显示票证详细信息的票证(TicketDetail 组件)。 重定向有效 - 它会将您带到正确的 url 并调度一个操作 (GET_TICKET)。

            <Link
              className="link"
              to={`/tickets/${ticket.id}`}
              onClick={() => this.getTicket(ticket.id)}
            >
              {ticket.id}
            </Link>

状态 (Redux) 已更新,出现正确的票证

但我的 TicketDetails 没有显示出来

import React, { PureComponent } from 'react'
import { connect } from 'react-redux'
import { getTicket } from "../../actions/tickets";
import { getUsers } from '../../actions/users'
import { userId } from '../../jwt'
import Paper from 'material-ui/Paper'


class TicketDetails extends PureComponent {

    componentWillMount() {
        if (this.props.authenticated) {
            if (this.props.tickets === null) this.props.getTicket();
            if (this.props.users === null) this.props.getUsers()
        }
    }

    render() {
        const { ticket, users } = this.props


        if (ticket === null || users === null) return 'Loading...'
        if (!ticket) return 'Not found'


        return(
            <div>
            <Paper className="outer-paper">
            <h1>Ticket: {ticket.id}</h1> {/* should display authors name */}
            <p>Price: {ticket.price}</p>
            <p>Description: {ticket.description}</p>
            <hr />
          </Paper>
            </div>

          )

    }
}

const mapStateToProps = (state, props) => {
    console.log('price', state.tickets.price)
    console.log('ticket', state.ticket);
    console.log('id', props.match.params.id);
    return {
        authenticated: state.currentUser !== null,
        userId: state.currentUser && userId(state.currentUser.jwt),
        ticket: state.tickets && state.tickets[props.match.params.id],
        users: state.users,
    }
}

const mapDispatchToProps = {
    getTicket,
    getUsers
};

export default connect(mapStateToProps, mapDispatchToProps)(TicketDetails)

【问题讨论】:

  • 你说重定向有效,那么当你点击一个id时会显示什么。它只是一个空白页吗?

标签: reactjs redux react-redux


【解决方案1】:

您正在尝试在 mapStateToProps 中添加一个布尔值

const mapStateToProps = (state, props) => {
    console.log('price', state.tickets.price)
    console.log('ticket', state.ticket);
    console.log('id', props.match.params.id);
    return {
        authenticated: state.currentUser !== null,
        userId: state.currentUser && userId(state.currentUser.jwt),// here also boolean value will be added
        ticket: state.tickets.filter((ticket) => (ticket.id === props.match.params.id)),
        users: state.users,
    }
}

更新***

const mapDispatchToProps = (dispatch) => {
     return {
            getTicket : () => dispatch(getTicket()),
            getUsers : () => dispatch(getUsers())
     }    


};

【讨论】:

  • 你能详细说明一下吗?状态中的 jwt 应该在那里,所以到道具的映射应该只在“真”时发生,对吗?你能举一个更好的方法来写这个的例子吗?我对 react-redux 还很陌生,所以我真的不知道如何重构它……就像将数据从我的 redux 状态返回到组件中一样。
  • 您正在使用可变票证做两种事情。如果 (!ticket) 返回“未找到”和 {ticket.description}。在第一种情况下,您将其用作布尔值,在第二种情况下,将其用作对象
  • 当您将状态映射到道具时,您正在使用布尔值填充道具状态变量。那么当你保存一个布尔值时,你怎么能从中获取数据呢?
  • 当你是新手时,不要使用这样的东西让事情变得复杂—— const { ticket, users } = this.props
  • 但是通过检查是否 (!ticket) 不是检查对象是否进来?复杂的东西,你的意思是解构吧?好吧,在其他情况下,这很有效。所以我认为它应该在这种情况下工作。那么你将如何着手,从状态中获取数据并将其呈现在组件中呢?
猜你喜欢
  • 2016-07-05
  • 1970-01-01
  • 1970-01-01
  • 2019-05-21
  • 1970-01-01
  • 1970-01-01
  • 2014-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多