简体   繁体   English

无法在反应组件内的 html 中打印 javascript 变量文本

[英]Unable to print javascript variable text in html inside a react component

I'm learning a backend administrative dashboard framework callled admin bro.我正在学习一个名为 admin bro 的后端管理仪表板框架。 So basically i am trying to make a tooltip component.所以基本上我正在尝试制作一个工具提示组件。 And this is the code I have tried till now.这是我到目前为止尝试过的代码。

class Textbox extends React.PureComponent {
 

render() {
    const { property } = this.props;
    
    const ob = this.props.property.name
    const sentence = { 
        first : ' this is the first text ',
        second : ' this is second text',
        third: 'this is third text',
        fourth : 'this is the fourth text',
    }

    
    var line = sentence[ob]
       
    return (
        <script type="text/javascript">
            document.write(line)
        </script>
    )
  }
}

export default withTheme(Textbox)

In this ob has the values like first,second, third and fourth, so when it is sentence[ob] it means for example sentence[first] (when ob is first) so line then gets the corresponding text to it which is 'this is the first text'.在这个ob中具有第一、第二、第三和第四等值,所以当它是sentence[ob]时,它意味着例如 sentence[first](当 ob 是第一个时)所以line然后获取相应的文本,即 'this是第一个文本'。 I have consoled logged line and seen that this part is working perfectly.我已经对记录的line进行了安慰,并看到这部分工作正常。

Now I just have to display line inside the html code inside return.现在我只需要在 html 代码内显示line返回。

I have tried to implement document.write() from this - how to display a javascript var in html body我试图从这里实现document.write() - 如何在 html 正文中显示 javascript var

But this doesn't work.但这不起作用。 What can I do?我能做些什么?

I suggest you start with the React tutorials , it doesn't seem like you understand the syntax of React at all.我建议你从React 教程开始,看起来你根本不理解 React 的语法。 In the render() method, you need to return jsx, which looks a lot like HTML but with slight differences.在 render() 方法中,需要返回 jsx,它看起来很像 HTML 但略有不同。 I don't know what you want to return, so I gave an example of what you can return, a div with the line variable you generated in your code.我不知道你想返回什么,所以我举了一个你可以返回的例子,一个带有你在代码中生成的 line 变量的div

Also, you need to note that the render() method runs every time there is an update to the virtual DOM, so you should only place logic in it if it's absolutely necessary.此外,您需要注意,每次更新虚拟 DOM 时都会运行 render() 方法,因此您应该只在绝对必要时将逻辑放入其中。

class Textbox extends React.PureComponent {
    const { property } = this.props;
    
    const ob = property.name
    const sentence = { 
        first : ' this is the first text ',
        second : ' this is second text',
        third: 'this is third text',
        fourth : 'this is the fourth text',
    }

    
    var line = sentence[ob]
    render() {   
        return (
           <div className="line">{line}</div>
        )
    }
}

export default withTheme(Textbox)

The return method should always have one html parent tag (div in my example).返回方法应始终有一个 html 父标记(在我的示例中为 div)。 This element can have as many children as you like, but can't have any siblings.这个元素可以有任意多的孩子,但不能有任何兄弟姐妹。

Then you can execute javascript statements by wrapping them in curly braces like so {javascript goes here}然后,您可以执行 javascript 语句,方法是将它们包裹在花括号中,如下所示{javascript goes here}

So your code should be所以你的代码应该是

return(
  <div>{line}</div>
)

React is using a template syntax called Mustache ({ }) single curly brackets to replace the javascript variable inside JSX element. React 使用称为 Mustache ({ }) 单花括号的模板语法来替换 JSX 元素内的 javascript 变量。

After that React does the rendering together with the value that you have binded using the embedded expression.之后,React 将与您使用嵌入表达式绑定的值一起进行渲染。

Embedding Expressions in JSX 在 JSX 中嵌入表达式

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

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