简体   繁体   English

流星React如何使用传入的参数?

[英]Meteor React how to use passed in params?

I have two params that I have passed but I am unsure how to use it in the following example. 我已经通过了两个参数,但是不确定在以下示例中如何使用它。 Please help if you can. 如果可以的话请帮忙。

updater(layer, item){   
    this.setState({layer5: <img id="layer5" className="on-top img-responsive center-block" name="layer5" src="images\5.png" />});
}

So here is my code at the moment, what I would like to do is something like the following: 所以这是我现在的代码,我想做的事情如下:

updater(layer, item){   
    this.setState({{layer}: <img id={layer} className="on-top img-responsive center-block" name={layer} src={item.src} />});
}

But this gives me a syntax error. 但这给了我一个语法错误。

UPDATE: After the changes I am getting the correct values passed in but the state is not updating for some reason: 更新:更改后,我获得了正确的值,但是由于某种原因,状态没有更新:

    constructor(){
     super();   
         this.renderView = this.updater.bind(this);
         this.state = {
                 layer1: "1.png",
                 layer2: "",
                 layer3: ""
         };
    }


      updater(layer, item){   
            console.log(layer); //shows as "layer1"
            console.log(item);  // shows as "3.png"
            this.setState({layer: item});
     }

You should not store React components in your state. 您不应该在您的状态下存储React组件。 Instead, store the information required to do the rendering and actually render the components in the render() function: 而是在render()函数中存储进行渲染并实际渲染组件所需的信息:

updater(layer, item){   
    this.setState({
        layer5: {
            layer: layer,
            item: item
        }
    });
 }

 render() {
     var layer5 = (
         <img
             id={this.state.layer5.layer}
             className="on-top img-responsive center-block"
             name={this.state.layer5.layer}
             src={this.state.layer5.item}
          />
      )

      ...
}

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

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