所以我有一個每 60 個字符添加一個換行符的功能。現在我不明白的是為什么 \n 不起作用。
function countStr (str1){
let str2="";
let charCount=0;
for(let i=0; i<str.length; i ){
if(i%60===0){
str2=str2.concat(str1.substring(charCount,i));
str2 ="\n";
charCount=i;
}
}
return str2;
}
const About = () =>{
return(
<div className="about">
<h2>I'm Gal</h2>
<p>{changedStr}
</p>
</div>
);
}
export default About;
uj5u.com熱心網友回復:
回圈輸入字串。如果當前迭代在i % 20(并且也不為零)添加換行符。添加輸入字串中的字符。
在你的組件的渲染字串中,你需要一些CSS添加到元件,使得它列印正確:white-space: pre-line。
function countStr(str) {
let out = '';
for (let i = 0; i < str.length; i ) {
if (i % 20 === 0 && i !== 0) out = '\n';
out = str[i];
}
return out;
}
// Example string of 220 characters
let str = '';
for (let i = 0; i < 220; i ) {
str = '1';
}
function Example({ str }) {
return (
<div>{str}</div>
);
};
ReactDOM.render(
<Example str={countStr(str)} />,
document.getElementById('react')
);
div { white-space: pre-line; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="react"></div>
uj5u.com熱心網友回復:
使用這個簡單的功能
function countStr(str) {
let str2 = '';
while (str.length > 0) {
str2 = str.substring(0, 60) '\n';
str = str.substring(60);
}
return str2;
}
uj5u.com熱心網友回復:
如果您在 React 中使用它并且它正在訪問網路,那么那是因為您可能需要一個<br/>不是 \n
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/351792.html
標籤:javascript 反应
