简体   繁体   English

Reactjs CSS数字之间的空白

[英]Reactjs css white-space between numbers

Im beginner on Reactjs and trying to learn and improve, here i have code where is < h1 >test< / h1 > and under this should appear numbers under each other like this 1:1 1:2 1:3, but css does not seem to work with it, i get numbers but without css and i dont get any error message either... is here something wrong ? 我是Reactjs的初学者并尝试学习和改进,在这里我有代码在哪里<h1> test </ h1>,并且在此之下应该出现数字,例如1:1 1:2 1:3,但是css没有似乎可以使用它,我得到的是数字但没有CSS,或者我也没有收到任何错误消息...这里有问题吗? the code : 编码 :

 import React, { Component } from 'react' class Button extends Component { state = {} button = () => { const proxyurl = "https://cors-anywhere.herokuapp.com/"; const url = "http://*****.*****.com/numbers.txt"; fetch(proxyurl + url) .then(response => response.text()) .then(contents => document.write(contents)) } render() { return ( <div > <h1>test</h1> <div style={{ color: 'red' }}>{this.button()} </div> </div > ); } } export default Button; 

css: CSS:

 body { background: url('***.png'); color:red; margin:50px 0; padding:0px; text-align:center; } #root { white-space: pre; } 

Your render function should be pure , see https://reactjs.org/docs/react-component.html#render : 您的渲染函数应该是函数,请参阅https://reactjs.org/docs/react-component.html#render

The render() function should be pure, meaning that it does not modify component state, it returns the same result each time it's invoked, and it does not directly interact with the browser. render()函数应该是纯函数,这意味着它不会修改组件状态,它在每次调用时都返回相同的结果,并且不与浏览器直接交互。

Your render function contains a call to this.button . 您的渲染函数包含对this.button的调用。 So every time your component re-renders, a fetch request is made when it seems this should only be called once. 因此,每次您的组件重新渲染时,都会在似乎仅应调用一次时发出提取请求。 As the docs suggest, move this logic into componentDidMount . 正如文档所建议的,将此逻辑移至componentDidMount


Now, onto your actual problem. 现在,进入您的实际问题。 You are calling document.write , and it seems you don't understand how this works. 您正在调用document.write ,似乎您不知道它是如何工作的。 Document.write will remove all event listeners from the page and replace all content within body with the argument you supplied. Document.write将从页面中删除所有事件侦听器,并将body所有内容替换为您提供的参数。 Assuming you had a root element with an ID of root ( <div id="root">...</div> ), that will have been removed after your call to document.write ; 假设您有一个根元素,其root ID为root<div id="root">...</div> ),那么在调用document.write之后,该元素将被删除; so your CSS #root selector will no longer point to an existing element. 因此您的CSS #root选择器将不再指向现有元素。

Instead of using document.write , set the content on your component's state and render that: 而不是使用document.write ,而是在组件的状态上设置内容并进行渲染:

import React, { Component } from "react";

export default class Button extends Component {
  state = {
    contents: null
  };

  componentDidMount() {
    const proxyurl = "https://cors-anywhere.herokuapp.com/";
    const url = "http://*****.*****.com/numbers.txt";
    fetch(proxyurl + url)
      .then(response => response.text())
      .then(contents => this.setState({ contents }));
  }

  render() {
    return (
      <div>
        <h1>test</h1>
        <div style={{ whiteSpace: "pre" }}>{this.state.contents}</div>
      </div>
    );
  }
}

If you're using React, you should have no reason to call document.write , even if you're doing it for testing or you're trying to implement some sort of page reload / turbolinks feature–there are much better alternatives. 如果您使用的是React,即使您正在进行测试或正在尝试实现某种页面重新加载/ turbolinks功能,也没有理由调用document.write ,这是更好的选择。

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

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