使用上下文API时没有任何渲染

时间:2020-09-08 10:14:37

标签: arrays reactjs react-hooks render

return (
            <div className="container-fluid" style={{ marginTop: '72px' }}>
                {/* {button}
                <div className="Cards">{successCards}</div>
                <Modal show={this.state.calendar} modalClosed={this.closeCalendar}>
                    {showCalendar}
                </Modal> */}
                <CalenderContext.Consumer>
                    {context=>{
                        if(context.result.length){
                            context.result.map((successCard) => {
                                return (
                                    <Card
                                        key={successCard.fileName}
                                        image={successCard.imageUrl}
                                        payerName={successCard.result.payer.payerName}
                                        subscriberName={successCard.result.subscriberName.subscriberName}
                                        subscriberId={successCard.result.subscriberId.subscriberName}
                                    />
                                );
                            });
                        }
                    }}
                </CalenderContext.Consumer>
            </div>
        );

我正在以数组形式正确获取上下文。一切都很好,但是什么也没渲染。任何人都可以帮助解决该问题。

1 个答案:

答案 0 :(得分:1)

使用react Fragment解决了问题。

<CalenderContext.Consumer>
                    {context=>(
                        <Fragment>
                            {context.result.map((successCard) => {
                                return (
                                    <Card
                                        key={successCard.fileName}
                                        image={successCard.imageUrl}
                                        payerName={successCard.result.payer.payerName}
                                        subscriberName={successCard.result.subscriberName.subscriberName}
                                        subscriberId={successCard.result.subscriberId.subscriberName}
                                    />
                                );
                            })}
                        </Fragment>
                    )}
                </CalenderContext.Consumer>

谢谢。