简体   繁体   English

在 ReactJs 中使用 onclick 函数和 setState 时,如何传递参数?

[英]In ReactJs when use onclick fuction with setState, how to pass parameters?

In my case, im using few buttons and when a button is clicked, there should be a change in view.就我而言,我使用的按钮很少,当单击按钮时,视图应该会发生变化。 for the view i am using same panel for each button click and the change will be apply on heading of the panel( company name).对于视图,我为每个按钮单击使用相同的面板,更改将应用​​于面板的标题(公司名称)。 To change the name I am sending parameter with Onclick method like below.要更改名称,我使用 Onclick 方法发送参数,如下所示。

class View extends Component {
    constructor(props){
        super(props)
        this.state ={
            message: <Content name="ABC"/>
        }
    }

    changeStateMSG(prevStep,props){
        this.setState({
            message:<Content name={this.props.name}/>
        })
    }
    render() { 
        return ( 
            <div className="row">
            <div className="mr-sm-3">
            <div style={{ width: '18rem', marginTop: '20px' }}>

              <Button onClick={() => this.changeStateMSG(this.props.name="XYZ")} variant="secondary" size="lg" block >
                    Omobio
              </Button>

              <div className="mr-sm-9">
                  <p>{this.state.message}</p>
              </div>

.... when i pass parameter like above (this.props.name="XYZ") i am getting error as "TypeError: Cannot add property name, object is not extensible". .... 当我传递像上面这样的参数 (this.props.name="XYZ") 我收到错误为“TypeError:无法添加属性名称,对象不可扩展”。 Hope some can help me希望有人能帮助我

You cannot change the props as the react docs says 你不能像反应文档所说的那样改变props

Props are Read-Only 道具是只读的

Whether you declare a component as a function or a class, it must never modify its own props 无论您将组件声明为函数还是类, 都不得修改其自身的道具

You should just pass "XYZ" . 您应该只传递"XYZ" or you can pass a object with this.props with name:'XYZ' using spread operator. 或者您可以使用传播运算符传递name:'XYZ' this.props对象。

onClick={() => this.changeStateMSG({...this.props,name:"XYZ"})}

And change your function which this 并改变你的功能

changeStateMSG(prevStep){
        this.setState({
            message:<Content name={prevState.name}/>
        })
    }

If you don't want all the props object inside your function then just pass "XYZ" . 如果您不希望函数中包含所有props对象,则只需传递"XYZ"

onClick={() => this.changeStateMSG("XYZ")}

And change you function to this 并改变你的功能

changeStateMSG(name){
   this.setState({
            message:<Content name={name}/>
   })
}

this.props is what View receives from its parents. this.propsView从其父级收到的内容。 You cannot set a value to it. 您不能为其设置值。 use instead: 改用:

onClick={() => this.changeStateMSG("XYZ")}

And

changeStateMSG(value){
    this.setState({
        message:<Content name={value}/>
    })
}

Have you checked the documentation? 您检查过文档了吗? I believe the error clearly states that you can not add property to the this.props . 我相信错误明确指出您无法将属性添加到this.props

You can pass the arguments just with this statement, like in a normal function execution. 您可以仅使用此语句传递参数,就像在常规函数执行中一样。

<Button 
  onClick={() => this.changeStateMSG("value_first_argument", "value_ second_argument")}
  variant="secondary"
  size="lg"
  block >
  Omobio
</Button>

And your function definition should look like, 您的函数定义应如下所示:

changeStateMSG(prevStep, props){
        this.setState({
            message:<Content name={this.props.name}/>
        })
    }

Why you have to pass the props.name to changeStateMSG function when you can access it from that function? 为什么你必须将传递props.namechangeStateMSG功能时,你可以从该函数访问它? Just simple call that in changeStateMSG and remove parameters you don't use. 只需在changeStateMSG调用它并删除不使用的参数即可。

changeStateMSG(){
    this.setState({
        message:<Content name={this.props.name}/>
    })
}

...
onClick={this.changeStateMSG}

Problem is function this.changeStateMSG(this.props.name="XYZ"). 问题是函数this.changeStateMSG(this.props.name =“ XYZ”)。 Did U pass variable prevStep. U是否传递了变量prevStep。 U pass like this (this.props.name="XYZ") is wrong way. 这样的U传递(this.props.name =“ XYZ”)是错误的方式。 Should edit: 应该编辑:

render() { 
     const prevStep = 1;
     return ( 
        <div className="row">
          <div className="mr-sm-3">
            <div style={{ width: '18rem', marginTop: '20px' }}>

              <Button onClick={() => this.changeStateMSG(prevStep, {name:'XYZ'})}  variant="secondary" size="lg" block >
                    Omobio
              </Button>

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

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