Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 试图在react jsx中为this.state指定一个常量_Javascript_Reactjs_React Redux_Lightbox_Jsx - Fatal编程技术网

Javascript 试图在react jsx中为this.state指定一个常量

Javascript 试图在react jsx中为this.state指定一个常量,javascript,reactjs,react-redux,lightbox,jsx,Javascript,Reactjs,React Redux,Lightbox,Jsx,因此,我试图在常量中创建一个图像光盒,但每当我尝试设置状态时,它都不允许我这样做,相反,我不断得到一个错误,即“isOpen”被指定了一个值,但从未使用过 const LineItem = ({ currency, lineItem, model, prints, snapshot }) => { if (!lineItem) { return null; } const { estimates, itar, quantity, uri } = lineItem;

因此,我试图在常量中创建一个图像光盒,但每当我尝试设置状态时,它都不允许我这样做,相反,我不断得到一个错误,即“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 LineItem=({currency,LineItem,model,prints,snapshot})=>{
如果(!lineItem){
返回null;
}
const{estimates,itar,quantity,uri}=lineItem;
此.state={
伊索彭:错,
};
const{isOpen}=this.state;
返回(
this.setState({isOpen:true})
>
打开灯箱
this.setState({isOpen:false})
/>
);
};

我如何使它在这个
常量中设置状态
每当我加载页面时,我想加载的灯箱都会加载,但它会卡在特定的状态中,我无法用鼠标垫滚动页面,因为它仍然认为图像是他们的,即使它不是

您实际上没有在任何地方使用isOpen! 设置state不需要定义常量

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


try just state={isOpen:false},如果没有此选项,则错误是准确的。您没有在任何地方使用常量,因此不必麻烦分配它。@jmargolisvt是否有方法在代码中使用它?@BrunoMazzardo我将所有值从this.state或this.setState更改为just state,但我仍然得到一个错误,即我的“isOpen”被分配了一个从未使用过的值。我不知道你在寻找什么答案。当然,您可以使用它,但这里的代码并不需要它。只需删除
const{isOpen}=this.state你应该很好。