【问题标题】:How to properly add a break in JSX file for displaymessage?如何在 JSX 文件中正确添加显示消息的中断?
【发布时间】:2022-01-07 09:00:35
【问题描述】:
handleNotFound() {
    this.setState({
        displayMessage:
            'It looks like the link you are trying to reach does not exist.\n      ' +

            'If you clicked this link , please leave a comment within the article that contained the link so the author can be notified.\n       ' +

            'If you clicked this link from a location outside of __, please contact the _to open a ticket with the application where this link is referenced .\n      ' +
            
            'The link you were trying to reach:     ' +
            url
    });

显示信息最后出现在一个句子中,我如何正确地在每个句子之间添加一个中断?

【问题讨论】:

    标签: javascript html jsx


    【解决方案1】:

    对于标准消息之类的东西,我更倾向于将它们放在配置文件中而不是状态中。配置文件可以包含任何内容:对象、数组、返回数组的函数...

    后者是我在这段代码中使用的,因为您有时需要填写url。该函数接受一个 url 参数,并在返回数组时填充该参数。然后您可以map 覆盖数组并显示每一行。

    const { Component } = React;
    
    const config = {
      displayMessage: function (url) {
        return [
          'It looks like the link you are trying to reach does not exist.',
          'If you clicked this link , please leave a comment within the article that contained the link so the author can be notified.',
          'If you clicked this link from a location outside of __, please contact the _to open a ticket with the application where this link is referenced.',
          `The link you were trying to reach: ${url}`
        ]
      }
    }
    
    class Example extends Component {
    
      constructor() {
        super();
        this.url = 'http://www.google.com';
      }
    
      render() {
        return (
          <div>
            {config.displayMessage(this.url).map(line => {
              return <p>{line}</p>;
            })}
          </div>
        );
      }
    
    };
    
    ReactDOM.render(
      <Example />,
      document.getElementById('react')
    );
    <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>

    【讨论】:

      【解决方案2】:
      handleNotFound() {
          this.setState({
              displayMessage: `
                something went wrong.
                I think you should use html tags 
                to render your error message if it is in the dom
              `
          });
      

      // Jsx 不会打断你的段落,但你有两个选择

      1. 将displayMessage 包裹在pre 标记中。 `

        {this.state.displayMessage}
      2. 拆分、映射和修剪字符串。

      render(){
        return this.state.displayMessage.split("\n").map(sentence => (
          <p>{sentence.trim()}</p>
        ))
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-12
        • 2021-02-13
        • 1970-01-01
        • 2022-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-27
        相关资源
        最近更新 更多