简体   繁体   English

.toUpperCase()不起作用

[英].toUpperCase() not working

I am building a webapp for a class where you can enter in a city and state and it displays the 5 day forecast. 我正在为一个班级构建一个Webapp,您可以在其中输入城市和州,并显示5天的天气预报。 Under that it shows you the last three cities you searched for. 在其下显示了您搜索的最近三个城市。 What I can not figure out is why when I do {this.state.city1.toUpperCase()} does not work. 我无法弄清楚的是为什么当我执行{this.state.city1.toUpperCase()}时不起作用。

Any suggestions? 有什么建议么?

class App extends Component {
  constructor(props){
    super(props);
    this.state = {
      city: ``,
      currentState: ``,
      tenDays:[],
      status:``,
      array1 : [],
      array2 : [],
      array3 : [],
      city1: ``,
      city2: ``,
      city3: ``,


    }
    this.handleChange = this.handleChange.bind(this);
    this.cityHandleChange = this.cityHandleChange.bind(this);
    this.go = this.go.bind(this);

  }

  render() {

    return (
      <div className='App'>
        <header className='App-header'>
          &lt;DevWeather /&gt;
        </header>
        <City cityHandleChange = {this.cityHandleChange} city = {this.state.city}/>
        <State handleChange = {this.handleChange} currentState = {this.state.currentState}/>
        <GetWeather go = {this.go}/>
        <h3>{this.state.status}</h3>
        <div className = "weatherCardsContainer">
        {weathercard}
        </div>
        <h3>Recent Searches</h3>
        <div className = "recents">
          <div className = "recentsCard" onClick = {() => {this.inputRecent1()}}>
            <a>{this.state.city1}</a>
          </div>
          <div className = "recentsCard" onClick = {() => {this.inputRecent2()}}>
            <a>{this.state.city2}</a>
          </div>
          <div className = "recentsCard" onClick = {() => {this.inputRecent3()}}>
            <a>{this.state.city3}</a>
          </div>
          {/* {recents} */}
        </div>
      </div>
    )
  }
}
export default App;

toUpperCase is a prototype method defined for a string , if city1 is not a string it will not work, make sure that this.state.city1 is not null or undefined before using toUpperCase . toUpperCase是为string定义的prototype method ,如果city1 不是字符串 ,将无法使用,请在使用toUpperCase之前确保this.state.city1不为nullundefined You might need to do the following 您可能需要执行以下操作

 {
     (typeof this.state.city1 === 'string' 
     || this.state.city1 instance of String) 
     ? this.state.city1.toUpperCase() 
     : null
 }

Try changing the default state in the constructor for the items using: `` as the default value (city, status, city1, etc.). 尝试使用以下项目更改项目构造函数中的默认状态: ``作为默认值(city,state,city1等)。 Changing them to use regular double or single quotes instead should fix your issue. 将其更改为使用常规双引号或单引号可以解决您的问题。 So `` Should be: "" or '' 所以``应该是: ""''

As far I see, you have not defined this.handleChange method. 据我所知,您尚未定义this.handleChange方法。

I imagine that you want to have like a history of search. 我想您想拥有一段搜索历史。

I'd recommend to write a handler like this: 我建议编写这样的处理程序:

handleChange(e) {
  e.preventDefault();

  this.setState({
    city: e.target.value,
    history: [...this.state.history, e.target.value]
  });
}

Then you can use lodash to show the last three records in your history array: 然后,您可以使用lodash在历史记录数组中显示最后三个记录:

{_.times(3).map((i) => ((
  <div
    key={i}
    className="recentsCard"
    onClick={() => {this.inputRecent(i)}}
  >
    <a>{this.state.history[this.state.history.length - i - 1].toUpperCase()}
   </a>
  </div>
))}

I didn't test it. 我没有测试。 So let me know if you need more help on that. 因此,如果您需要更多帮助,请告诉我。

Good luck! 祝好运!

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

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