Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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
String 添加组件';在React中将状态转换为字符串_String_Reactjs_Components - Fatal编程技术网

String 添加组件';在React中将状态转换为字符串

String 添加组件';在React中将状态转换为字符串,string,reactjs,components,String,Reactjs,Components,我有一个具有以下状态的组件: class Example extends Component { constructor(props) { super(props); this.state = { var1: "Dog", var2: "Cat", var3: [20, 40], currentFilter:"None" }; this.updateFilter = this.updateFilter.bi

我有一个具有以下状态的组件:

class Example extends Component {
  constructor(props) {
    super(props);
    this.state = { 
      var1: "Dog",
      var2: "Cat",
      var3: [20, 40],
      currentFilter:"None"
    };
      this.updateFilter = this.updateFilter.bind(this);
  }
在update filter函数中,我希望合并所有状态属性,但以下语法不起作用:

updateFilter(){
  var newSearch= "Searching" {this.state.var1} + {this.state.var2}
  this.setState({
    currentFilter: newSearch
  });
}

有没有办法将状态的属性合并到字符串变量中?

除非编写JSX,否则不需要大括号。由于您的
updateFilter()
函数只是一个普通的Javascript函数,因此您可以将其编写为:

updateFilter(){
var newSearch=“search”+this.state.var1+this.state.var2;
这是我的国家({
currentFilter:newSearch
});
}
不过,仅供参考,
newSearch
的结果将是不连贯的
“SearchingDogCat”
,因此您可能需要重新考虑连接

您也可以这样做:

updateFilter() {
  var newSearch = `Searching ${this.state.var1} ${this.state.var2} `;
  this.setState({
    currentFilter: newSearch
  });
}

请注意,这些不是单引号。它们是`

谢谢!我使用的例子只是我的应用程序的一个非常简化的裸体版本,但我的观点是正确的。