简体   繁体   English

如何从反应 state 向文本添加换行符

[英]how do i add a line break to a text from react state

So here's a perfectly working react code所以这是一个完美的反应代码

const about={
  text1: 'Lorem, ipsum dolor sit amet consectetur adipisicing elit. Facere, qui?',
  text2: 'eligendi voluptates nesciunt quam dolor reiciendis dolorum voluptas? Labore, a pariatur?'
}

function App() {

  const [text, setText] = useState(about)
  const [switchh, setSwitch] = useState(false)

  return (
    <div className="App">
      <button onClick={() => setSwitch(!switchh)}>switch</button>
     <h1>{switchh ? text.text1 : text.text2}</h1>
    </div>
  );
}

export default App;

this code is working as it should however I can't seem to find a way to add line break to it.这段代码可以正常工作,但是我似乎找不到添加换行符的方法。 I want it to look like this我希望它看起来像这样

<h1>Lorem, ipsum dolor sit amet<br/> consectetur adipisicing elit. Facere, qui?</h1>

how do i achieve that while using state cause I need the switch also我如何在使用 state 时实现这一点,因为我也需要开关

Try this approach,试试这个方法,

Use html as value to the text1 itself like below,使用html作为text1本身的值,如下所示,

const about = {
  text1: (
    <h1>
      Lorem, ipsum dolor sit amet
      <br /> consectetur adipisicing elit. Facere, qui?
    </h1>
  ),
  text2:
    "eligendi voluptates nesciunt quam dolor reiciendis dolorum voluptas? Labore, a pariatur?"
};

Complete Code:-完整代码:-

import React, { useState } from "react";
import "./styles.css";

const about = {
  text1: (
    <h1>
      Lorem, ipsum dolor sit amet
      <br /> consectetur adipisicing elit. Facere, qui?
    </h1>
  ),
  text2:
    "eligendi voluptates nesciunt quam dolor reiciendis dolorum voluptas? Labore, a pariatur?"
};

export default function App() {
  const [text, setText] = useState(about);
  const [switchh, setSwitch] = useState(false);

  return (
    <div className="App">
      <button onClick={() => setSwitch(!switchh)}>switch</button>
      <h1>{switchh ? text.text1 : text.text2}</h1>
    </div>
  );
}

Working code - https://codesandbox.io/s/determined-feather-qd7me?file=/src/App.js工作代码 - https://codesandbox.io/s/determined-feather-qd7me?file=/src/App.js

If you can change the content of about object, and if you don't mind to add some css for the element, you can try this:如果你可以更改关于object的内容,并且如果你不介意为元素添加一些css,你可以试试这个:

const about={
  text1: 'Lorem, ipsum dolor sit amet \n consectetur adipisicing elit. Facere, qui?',
  text2: 'eligendi voluptates nesciunt \n quam dolor reiciendis dolorum voluptas? Labore, a pariatur?'
}

... 

<h1 className='my-text'>{switchh ? text.text1 : text.text2}</h1>

and the css:和 css:

.my-text {
  white-space: pre-wrap;
}

Storing a react element in state is not a good practice and try not to use reserved words like switch .在 state 中存储一个反应元素不是一个好习惯,尽量不要使用像switch这样的保留字。 If a state holds a boolean value, a good practice is to name the variable starting with 'is' or 'has'.如果 state 包含 boolean 值,则一个好的做法是以“is”或“has”开头的变量命名。

My solution:我的解决方案:

import React, { useState } from "react";
import "./styles.css";

const about = {
  text1: (
    <h1>
      Lorem, ipsum dolor sit amet
      <br /> consectetur adipisicing elit. Facere, qui?
    </h1>
  ),
  text2:
    "eligendi voluptates nesciunt quam dolor reiciendis dolorum voluptas? Labore, a pariatur?"
};

export default function App() {
  const [isSwitchedOn, setIsSwitchedOn] = useState(false);

  return (
    <div className="App">
      <button onClick={() => setIsSwitchedOn(!isSwitchedOn)}>switch</button>
      <h1>{isSwitchedOn ? about.text1 : about.text2}</h1>
    </div>
  );
}

you could use an array and map it to {text}你可以使用一个数组和 map 它来 {text}

import React, { useState } from "react";
import "./styles.css";

const about = {
  text1: [
    'Lorem, ipsum dolor sit amet',
     'consectetur adipisicing elit. Facere, qui?'
  ],
  text2:
    "eligendi voluptates nesciunt quam dolor reiciendis dolorum voluptas? Labore, a pariatur?"
};

export default function App() {
  const [text, setText] = useState(about);
  const [switchh, setSwitch] = useState(false);
console.log(text.text1)
  return (
    <div className="App">
      <button onClick={() => setSwitch(!switchh)}>switch</button>
      <h1>{switchh ? text.text1.map(line=>(<React.Fragment key={line}>{line}<br/></React.Fragment>)) : text.text2}</h1>
    </div>
  );
}

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

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