【问题标题】:Why is my column component not being displayed as expected?为什么我的列组件没有按预期显示?
【发布时间】:2022-01-03 10:53:54
【问题描述】:

我想使用<Row><Col> 组件来安排我网站上的内容。但是,内容没有按预期显示... :(

我做错了什么?

这是我的App.tsx class

import "./App.css";
import DropdownMenu from "./components/DropdownMenu";
import TextBox from "./components/TextBox";
import { Component } from "react";
import Col from 'react-bootstrap/Col';
import Row from 'react-bootstrap/Row';


interface AppProps { }

interface AppState { selectedCurrency: string }

class App extends Component<AppProps, AppState> {

  constructor(props: AppProps) {
    super(props)
    this.state = { selectedCurrency: "EUR" }
  }

  private getData = (val: any) => { this.setState({ selectedCurrency: val.value }) }

  render() {
    return (
      <div>
        <DropdownMenu sendData={this.getData} defaultValue="Currency" values={[{ label: "EUR", value: "EUR" }, { label: "GBP", value: "GBP" }, { label: "USD", value: "USD" }]} ></DropdownMenu>
        <DropdownMenu sendData={() => ""} defaultValue="Coin" values={[{ label: "Bitcoin", value: "Bitcoin" }, { label: "Etherum", value: "Etherum" }, { label: "Cardano", value: "Cardano" }, { label: "Polkadot", value: "Polkadot" }, { label: "Litecoin", value: "Litecoin" }, { label: "Enjin", value: "Enjin" }, { label: "Chilliz", value: "Chilliz" }, { label: "Vetchain", value: "Vetchain" }]} ></DropdownMenu><br></br>
        <TextBox title="Coinbase" currency={this.state.selectedCurrency} avgPrice={200} volume={10000} marketcap={21000} backgroundColor="gray" height={100} width={300}></TextBox>
        <Row>
          <Col>
            <TextBox title="Kraken" currency={this.state.selectedCurrency} avgPrice={200} volume={10000} marketcap={21000} backgroundColor="gray" height={100} width={300}></TextBox>
          </Col>
          <Col>
            <TextBox title="Kraken" currency={this.state.selectedCurrency} avgPrice={200} volume={10000} marketcap={21000} backgroundColor="gray" height={100} width={300}></TextBox>
          </Col>
        </Row>
// NEW LINES START
        <Container>
          <Row>
            <Col>1 of 2</Col>
            <Col>2 of 2</Col>
          </Row>
        </Container>
// NEW LINES END
      </div >
    );
  }
}
export default App;

结果如下: Output in my browser

【问题讨论】:

    标签: reactjs row react-bootstrap col


    【解决方案1】:

    我认为问题在于&lt;Col&gt; 的宽度属性是&lt;Row&gt; 之一。 通常,当您忘记 &lt;Container&gt; 时会发生这种情况。

    试试这个:

    <Container>
      <Row>
        <Col>1 of 1</Col>
      </Row>
    </Container>
    

    所以你的代码应该是这样的:

    render() {
        return (
            <Container>
                <DropdownMenu sendData={this.getData} defaultValue="Currency" values={[{ label: "EUR", value: "EUR" }, { label: "GBP", value: "GBP" }, { label: "USD", value: "USD" }]} ></DropdownMenu>
                <DropdownMenu sendData={() => ""} defaultValue="Coin" values={[{ label: "Bitcoin", value: "Bitcoin" }, { label: "Etherum", value: "Etherum" }, { label: "Cardano", value: "Cardano" }, { label: "Polkadot", value: "Polkadot" }, { label: "Litecoin", value: "Litecoin" }, { label: "Enjin", value: "Enjin" }, { label: "Chilliz", value: "Chilliz" }, { label: "Vetchain", value: "Vetchain" }]} ></DropdownMenu><br></br>
                <TextBox title="Coinbase" currency={this.state.selectedCurrency} avgPrice={200} volume={10000} marketcap={21000} backgroundColor="gray" height={100} width={300}></TextBox>
                <Row>
                    <Col>
                        <TextBox title="Kraken" currency={this.state.selectedCurrency} avgPrice={200} volume={10000} marketcap={21000} backgroundColor="gray" height={100} width={300}></TextBox>
                    </Col>
                    <Col>
                        <TextBox title="Kraken" currency={this.state.selectedCurrency} avgPrice={200} volume={10000} marketcap={21000} backgroundColor="gray" height={100} width={300}></TextBox>
                    </Col>
                </Row>
            </Container>
        );
    }
    }
    

    【讨论】:

    • 什么意思,. 不见了??我实现了以下内容:``` 1 of 2 2 of 2 ``` 但是,列是仍然显示在下面,而不是彼此相邻
    • 我在您的示例中没有看到
    • 是的,没错,它不存在。我在下面添加了我之前评论中的行。我将编辑我的原始帖子以展示在哪里:)
    • 如果此解决方案适合您,请不要忘记将其标记为已解决。
    • 它没有 :( 列仍然在彼此下方,而不是根据需要彼此相邻...我是不是又愚蠢又错过了什么?xD 感谢您抽出宝贵时间回答:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 2020-04-06
    • 2021-04-15
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多