【问题标题】:How to edit data received from a rest call in react?如何在反应中编辑从休息呼叫收到的数据?
【发布时间】:2021-02-20 12:07:48
【问题描述】:

我正在尝试打个休息电话,编辑数据然后渲染它。问题是,在编辑时,我收到一个错误 - 即使在检查数据是否存在之后也是未定义的。

我正在从以下位置进行其余调用的组件:

function Header ({timesheetData, loadTimesheet}) {    

    useEffect(() => {
        loadTimesheet(date)
    }, [])

    return(     
        <>
            <header className="header">
            <div className="wrap">
                <span className="btn-icon">
                    <IconPlus onClick={() => setIsOpen(true)} className="icon icon-plus js-modal-init"/>  
                </span>
                <div className="header-blockquote">
                    <h1 className="header-quote">{currentQuote.quote}</h1>
                    <div className="header-cite">{currentQuote.author}</div>
                </div>
            </div>
            <div className="header-inner">
                <div className="wrap">
                    <VegaIcon className="logo" alt="VegaIT"/>
                    <div className="date-wrap">
                        <IconCalendar className="icon icon-plus js-modal-init"/>
                        //
                        <time>{timesheetData.timesheet.loading ? "asd"  : edit(timesheetData)  }</time>
                        //
                    </div>
                </div>
            </div>
        </header>
            </>
        )
}


function edit(timesheetData){
    let newDate = timesheetData.timesheet.date
    newDate = newDate.split("-")
    newDate = newDate.reverse()
    return  newDate.join("/")
} 

redux 操作:

export const  loadTimesheet = (date) => {

    let url = "http://localhost:8080/api/timesheet/" + date

    return (dispatch) => {
        dispatch(getTimesheet)
        axios.get(url)
        .then((response) => {
            const timesheet = response.data
            dispatch(getTimesheetSuccess(timesheet))
        })
        .catch(error =>  {
            const errorMsg = error.message
            dispatch(getTimesheetFailure)
        })
    }
}

编辑:添加了我的 mapStateToProps 和 mapDispatchToProps

const mapStateToProps = state => {
    return {
        timesheetData: state.timesheet
    }
}

const mapDispatchToProps = dispatch => {
    return {
        loadTimesheet: (date) => dispatch(loadTimesheet(date))
    }
}

Edit2:代码:https://codesandbox.io/s/adoring-tharp-o9ibe

【问题讨论】:

    标签: reactjs redux axios


    【解决方案1】:

    使用mapStateToPropsmapDispatchToProps

    import getTimesheet from '../actions/...'
    
    Header ({timesheetData, loadTimesheet}) => {
     
    }
    
    const mapDispatchToProps = dispatch => {
      return {
        loadTimesheet: () => dispatch(getTimesheet()),
      }
    }
    
    const mapStateToProps = (state) => {
      return { timesheetData: state.timesheetData };
    };
    
    export default connect(mapStateToProps, mapDispatchToProps)(Header);  
    

    也在组件中:

    渲染前: 检查您是否拥有:

    • timesheetData - not undefined

    • timesheetData.timesheet - not undefined

    • timesheetData.timesheet.loading - not undefined

      const hasData = timesheetData && !timesheetData.timesheet.loading;
      const time = hasData ? edit(timesheetData): "asd";
      

    在渲染中:

    <time>{time}</time> 
    

    【讨论】:

    • 我已经这样做了,但没有任何改变。为了清楚起见,我将其排除在外。
    • 它不再给我未定义的错误,但它现在也没有进行其余的调用。很可能是因为 mapStateToProps 和 mapDispatchToProps 的变化。如果有帮助,我会将我以前的问题添加到问题中。
    • 我可以尝试这样做,但不确定它是否会工作,因为我使用的是 localhost api。
    • 你可以为 fetch 或 api 编写 mock,因为我认为它们可以工作
    猜你喜欢
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多