简体   繁体   English

如何在 JSX 中使用 amp-state?

[英]How to Use amp-state in JSX?

I'm working on creating Google amp-page in a React project, and need to fetch JSON after a click.我正在 React 项目中创建 Google amp-page,点击后需要获取 JSON。

A/C to amp.dev , i can use amp-state to fetch remote data A/C 到amp.dev ,我可以使用 amp-state 来获取远程数据

as作为

<amp-state id="shirts" [src]="'/shirts/sizesAndPrices?sku=' + selected.sku" />

How can i do the same in JSX, as writing [src] in such format leads to error我如何在 JSX 中做同样的事情,因为以这种格式编写 [src] 会导致错误

Any suggestion will be helpful.任何建议都会有所帮助。 Thanks in advance提前致谢

You can replace [src] with data-amp-bind-src .您可以将[src]替换为data-amp-bind-src The latter syntax has been introduced for JSX syntax compatibility.为 JSX 语法兼容性引入了后一种语法。

I don't know this is the right way, but I think, this can be a turn around If there is a better way, or if this is not recommended in any way, please comment我不知道这是正确的方法,但我认为,如果有更好的方法,或者以任何方式不推荐,请评论


I needed to toggle visibility of option based on the selected option我需要根据所选选项切换选项的可见性

<amp-selector
    layout="container"
    class="radio-selector"
    on="select: AMP.setState({
    selectedOption: event.targetOption,
})">
        <div option="a_show">Option A</div>
        <div option="b_show">Option B</div>
        <div option="c_show">Option C</div>
</amp-selector>
<div className="options"
    dangerouslySetInnerHTML={{
        __html: `
            <p [class]="selectedOption == 'a_show' ? 'show' : 'hide'" class="show">a</p>
            <p [class]="selectedOption == 'b_show' ? 'show' : 'hide'" class="hide">b</p>
            <p [class]="selectedOption == 'c_show' ? 'show' : 'hide'" class="hide">c</p>
        `
        }}
/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM