【问题标题】:Nothing is rendering when using context api使用上下文 api 时没有渲染
【发布时间】:2020-09-08 10:14:37
【问题描述】:
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>
        );

我以数组的形式正确获取上下文。一切都很好,但没有渲染。谁能帮忙解决这个问题。

【问题讨论】:

    标签: arrays reactjs react-hooks render


    【解决方案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>
    

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多