【问题标题】:ResourcePicker will not show资源选择器不会显示
【发布时间】:2019-05-13 18:39:45
【问题描述】:

我一辈子都无法打开 ResourcePicker。按下按钮时,我可以看到状态从 false 更改为 true,但资源选择器实际上并没有打开。

<AppProvider apiKey={apiKey} shopOrigin={shopOrigin} >
        <Page>
          <TopBar pageTitle="Create Sale" primaryButton="Save" clickPrimaryButton={this.handleClick} />
          <Layout sectioned={true}>
            <Layout.AnnotatedSection title="Scheduled sale settings" description="Setup the discount and which products will be selected for the sale.">
              <Card sectioned>
                <FormLayout>
                  <TextField label="Sale name" onChange={() => { }} />
                </FormLayout>
              </Card>

              <Card sectioned>
                <FormLayout>
                  <DiscountValue />
                </FormLayout>
              </Card>

              <Card>
                <Card.Section>
                  <FormLayout>
                    <SaleCategory onSelect={this.handleSelect} />
                  </FormLayout>
                </Card.Section>
                {searchBar}
                <Card.Section>
                  <Picker />
                </Card.Section>

              </Card>
            </Layout.AnnotatedSection>
          </Layout>
        </Page>
      </AppProvider>

然后是 Picker 组件

import React from 'react';
import { ResourcePicker, Button } from '@shopify/polaris';

class Picker extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      resourcePickerOpen: false,
    }
  }

  render() {
    return (
      <div>
        <pre>{JSON.stringify(this.state.resourcePickerOpen)}</pre>
        <ResourcePicker
          resourceType="Product"
          open={this.state.resourcePickerOpen}
          onSelection={({ selection }) => {
            console.log('Selected products: ', selection);
            this.setState({ resourcePickerOpen: false });
          }}
          onCancel={() => this.setState({ resourcePickerOpen: false })}
        ></ResourcePicker>
        <Button onClick={() => this.setState({ resourcePickerOpen: !this.state.resourcePickerOpen })}>Open...</Button>
      </div>
    );
  }
}

export default Picker;

我希望选择器打开,但它没有。我做错了什么?

【问题讨论】:

    标签: ruby-on-rails reactjs shopify-app polaris


    【解决方案1】:

    试着把你的状态声明像下面的代码 sn-p

    class Picker extends React.Component {
        state = {
           resourcePickerOpen: false,
        }  
    
        ...
    
    }
    

    你可以删除你的构造函数

    【讨论】:

    • 我认为这不是问题所在。我可以观察到状态的变化。
    猜你喜欢
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 2019-12-11
    相关资源
    最近更新 更多