javascript - React JS 根据用户输入显示月份

标签 javascript html reactjs react-redux frontend

我正在尝试根据用户输入显示月份名称。我有一个名为 Constants js 文件,其中定义了月份数组。 情况1: 当用户点击提交按钮在文本字段中输入 4 时,它应该显示四月 案例2: 当用户输入 33 个月数字时,它应该显示“无效月份数字”

Home JS file
<code>
import React, { Component }  from 'react';
import constants from './Constants';

class Home extends React.Component{
    constructor(props)
    {
        super(props);
        this.state ={
        value : ''
        }
        this.handleClick = this.handleClick.bind(this);
    }

    handleClick = (event) => 
    {
        this.setState({value: event.target.value})
    }

  render()
  {
    let newvalue = this.value;
     let latestvalue = constants.MONTHS[newvalue] == null ? 'invalid month' : constants.MONTHS[newvalue]  ; 
    return(
        <div className='home'>
        <h1>Welcome to my portfolio website</h1>
        Enter Month number <input type="text" value={this.state.value}/> 
        <button type="button" onSubmit={this.handleClick()}> </button>
        <p> Feel free to browse around and learn more about me.</p>
        Month is {latestvalue}
      </div>

     );
  }

}

export default Home;
</code>

Constants JS file

<code>

const constants = {

        MONTHS: ['','Jan','Feb','Mar','April','May','June','July']

}

export default constants;
</code>

It should display month or invalid month

我收到此错误“TypeError:无法读取未定义的属性“目标””

最佳答案

import React, { Component }  from 'react';
import constants from './Constants';

class Home extends React.Component{
    constructor(props)
    {
        super(props);
        this.state ={
        value : 0,
        month: constants.MONTHS[0]
        }
        this.handleClick = this.handleClick.bind(this);
    }

    handleClick = (event) => 
    {
        this.setState({value: event.target.value})
    }
    handleSubmit = () => {
        const month = constants.MONTHS[this.state.value] || 'invalid month'
        this.setState({
        month: month
       })
    }

  render()
  {

    return(
        <div className='home'>
        <h1>Welcome to my portfolio website</h1>
        Enter Month number <input type="text" onChange={this.handleClick} value={this.state.value}/> 
        <button type="button" onSubmit={this.handleSubmit}> </button>
        <p> Feel free to browse around and learn more about me.</p>
        Month is {this.state.month}
      </div>

     );
  }

}

export default Home;

关于javascript - React JS 根据用户输入显示月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54600482/

相关文章:

html - 如何从图标集图片中加载一个图标

html - 显示带有动态信息的 Adsense 广告

html - 我需要 float 或对齐还是什么?

javascript - 错误 : Uncaught TypeError: Cannot read properties of undefined (reading 'map' )

javascript - React.JS,组件之间传递数据

javascript - 基于 html 的网站中 PC 的真正唯一标识

javascript - React Router v6 错误 : All component children of <Routes> must be a <Route> or <React. 片段>

javascript - 更新 localStorage 中的项目时出现问题

javascript - 如何使用 Promise.all 等待两个请求,然后在回调中调用其他函数?

javascript - Rails Assets 管道 : JavaScript being executed twice