简体   繁体   English

我想用 react js 创建一些关于在窗口中添加字符串的程序,但是我遇到了错误

[英]I want to create some program with react js, about adding strings in window, but I am having an error

I am creating a program with React Js, where I need to write something in prompts and it needs to appear in window.我正在用 React Js 创建一个程序,我需要在提示中写一些东西,它需要出现在窗口中。 Now I have created the function of adding prompts and pasting it in window, but it's giving an error.现在我已经创建了添加提示并将其粘贴到窗口中的功能,但它给出了错误。 please help me if you can : )如果可以,请你帮助我 : )

export default class Clock extends React.Component {
   state = {items: ['Hakob', 'Arman']};

   Add(){
      const newitems = this.state.items.concat([prompt('a')])
      this.setState({items:newitems})
   }

 

   render(){
         return <div>
             <Clock2/> 
         </div>
   }
} 

class Clock2 extends React.Component {


   render(){
      return(
         <>
             <button onClick={this.Add}>click</button>
             
     


         <div> {this.state.items.map((e, i) => {
            return <div key = {e + i}> {e} </div>
         } )} </div>

         </>
      )
   }
}

you have not defined any state in class clock2 so, the line # 798 giving you an error for cannot read property of items as it is not defined in class您还没有在 clock2 类中定义任何状态,因此第 798 行给您一个错误,因为无法读取项目的属性,因为它没有在类中定义

class Clock2 extends React.Components {
state = {
items : // 
}

}

and the second error is you are trying to return in the return function that is not correct if you want to map items you have to define map function in render第二个错误是你试图在返回函数中返回,如果你想映射你必须在渲染中定义映射函数的项目,这是不正确的

{
const items = this.state.items.map((e,i ) => {
// 
}
return (
<items/>
)

So let's write your code here.所以让我们在这里写你的代码。

export default class Clock extends React.Component {
   state = { items: ['Hakob', 'Aram']};

   Add() {
      const newItems = this.state.items.concat([prompt('a')])
      this.setState({items:newItems})
   }

   render() {
      return <div><Clock2/><div>
   }
}

class Clock2 extends React.Component {
   render() {
      return (
         <>
            <button onClick={this.Add}>Click</button>
            <div>{ this.state.items.map( (e,i) => {
               return <div key={ e + i}>{e}</div>
            })}
            </div>
         </>
      )
   }
}

You made a mistake, state are internals to component, as well as method.你犯了一个错误,状态是组件的内部结构,也是方法。

This should work这应该工作

export default class Clock extends React.Component {
   state = { items: ['Hakob', 'Aram']};

   Add() {
      const newItems = this.state.items.concat([prompt('a')])
      this.setState({items:newItems})
   }

   render() {
      return (
          <>
            <button onClick={this.Add}>Click</button>
            <div>{ this.state.items.map( (e,i) => {
               return <div key={ e + i}>{e}</div>
            })}
            </div>
         </>
      )
   }
}

Or you can pass values from top component to his child.或者您可以将值从顶部组件传递给他的孩子。

export default class Clock extends React.Component {
   state = { items: ['Hakob', 'Aram']};

   Add() {
      const newItems = this.state.items.concat([prompt('a')])
      this.setState({items:newItems})
   }

   render() {
      return (<div><Clock2 add={this.Add} values={this.state.items}/><div>)
   }
}

class Clock2 extends React.Component {
   render() {
      return (
         <>
            <button onClick={this.prop.add}>Click</button>
            <div>{ this.props.values.map( (e,i) => {
               return <div key={ e + i}>{e}</div>
            })}
            </div>
         </>
      )
   }
}

暂无
暂无

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

相关问题 我想在电子 js 中为 about 应用程序(对于 windows)创建自定义子窗口 - I want to create custom child window for about application (for windows) in electron js React Native 我在从 state 添加数字时遇到问题 - React Native I am having issues with adding a number from state 想显示投资组合图片,但我遇到了一些错误 - want to show portfolio images but i am facing some error 我在运行 node.js 文件时出错 - I am having an Error in running a node.js file 我正在开发一个网站,我想在该网站中在我的js文件中本地读取一些json文件 - I am developing a website in which i want to read some json file locally in my js file 我无法理解为什么我无法映射我在 React 程序中设置的状态下返回的数据 - I am having trouble understanding why I am not able to .map over the data I am return in the state that I set up in my React program 我正在创建一个数组以响应打印图像 select 仅基于数字我想要的图像 - i am create an array in react to print images select only images i want based on to numbers 我的外部JS文件有问题 - I am having issues with an external JS file React JS:为什么我在 componentdidMount() 中出错? - React JS: Why am I getting error at componentdidMount()? 当我发送消息时,我正在尝试创建 web 聊天应用程序在控制台中出现此错误 - Am trying to create a web chat app when i send a message am having this error in console
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM