gpt4 book ai didi

javascript - react 中的文本溢出问题

转载 作者:行者123 更新时间:2023-11-28 02:27:04 26 4
gpt4 key购买 nike

当我尝试输入更多文本时,文本溢出并出现在水平线上。此处该行应根据文本自动调整

import React, { Component } from 'react';

export default class App extends Component {
constructor(props) {
super(props);
this.state = { items: [], text: '' };
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}

render() {
return (
<div>
<form onSubmit = {this.handleSubmit}>
<input onChange={this.handleChange} value={this.state.text} />
</form>
<div>
<TodoList items={this.state.items} />
</div>
</div>
);
}

handleChange(e) {
this.setState({ text: e.target.value });
}

handleSubmit(e) {
e.preventDefault();

if (!this.state.text.length) {
return;
}

const newItem = {
text: this.state.text,
};
this.setState(prevState => ({
items: prevState.items.concat(newItem),
text: ''
}));
}

class TodoList extends React.Component {
render() {
return (
<div>
{this.props.items.map(item => (
<h3 key={item.id}>{item.text}</h3>
))}
</div>
);
}
}

任何帮助将不胜感激,谢谢。

最佳答案

我认为你的代码中有一个小错误,你需要适本地声明输入标签的值。这是您可以尝试的代码:

import React, { Component } from 'react';

export default class App extends Component {
constructor(props) {
super(props);
this.state = { items: [], text: '' };
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}

render() {
return (
<div>
<form onSubmit = {this.handleSubmit}>
<input
onChange={this.handleChange}
value={this.state.text} />
</form>
<div>
<TodoList items={this.state.items} />
</div>
</div>
);
}

handleChange(e) {
this.setState({ text: e.target.value });
}

handleSubmit(e) {
e.preventDefault();
if (!this.state.text.length) {
return;
}
const newItem = {
text: this.state.text,

};
this.setState(prevState => ({
items: prevState.items.concat(newItem),
text: ''
}));
} }

class TodoList extends React.Component { render() { return (

<div>

{this.props.items.map(item => (
<h3 key={item.id} className="text-list">{item.text}</h3>
))}

</div>
);
}

}

render(<App />, document.getElementById('root'));

这是更新后的工作演示:https://react-guerkm.stackblitz.io/

您可以将下面给出的 css 添加到类名中:

.text-list{
color: aqua;
width: 50%;
overflow-wrap: break-word;
}

关于javascript - react 中的文本溢出问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47907163/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com