试图在 react jsx 中给出一个常量 a this.state

Trying to give a constant a this.state in react jsx

所以我试图在我的 const 中创建一个图像灯箱,但每当我尝试设置状态时它都不让我,而是我不断收到一个错误,指出 'isOpen' 被分配了一个值但从未使用过。

const LineItem = ({ currency, lineItem, model, prints, snapshot }) => {
  if (!lineItem) {
    return null;
  }
  const { estimates, itar, quantity, uri } = lineItem;
  this.state = {
    isOpen: false,
  };
  const { isOpen } = this.state;
  return (
    <Panel>
      <Col xs={12} sm={4}>
        <Row>
          <Col xs={10} xsOffset={1} lg={6} lgOffset={3}>
            <ModelThumbnail snapshot={snapshot} itar={itar} />
            <button
              type="button"
              onClick={() => this.setState({ isOpen: true })}
            >
              Open Lightbox
            </button>
            <Lightbox
              mainSrc={snapshot}
              onCloseRequest={() => this.setState({ isOpen: false })}
            />
          </Col>
        </Row>
    </Panel>
  );
};

我如何才能让它在此 const.
中设置状态我无法使用鼠标垫滚动页面的特定状态,因为它仍然认为图像是他们的,即使它不是。

您实际上并没有在任何地方使用 isOpen! 您无需为了设置状态而定义常量。

但主要问题是您试图更改无状态组件中的状态。尝试将其转换为 class 从 React 扩展组件。

https://reactjs.org/docs/react-component.html