简体   繁体   English

如何在 react.js 中创建 3 列布局结构

[英]How to create a 3 column layout structure in react.js

I have started learning about React js .我已经开始学习React js了。 Now I am building a blog page ideally a 3 column layout using React.js .现在我正在使用React.js构建一个理想的 3 列布局的博客页面。 Is there an efficient way of creating a simple 3 column layout, where I could style the columns.有没有一种有效的方法来创建一个简单的 3 列布局,我可以在其中设置列的style Created an App.js file and created files using Blog.js, BlogItem.js under components folder, but how to pass 3 column structure layout in React.js?创建了一个 App.js 文件,并在 components 文件夹下使用 Blog.js、BlogItem.js 创建了文件,但是如何在 React.js 中传递 3 列结构布局? Please advise请指教

// App.js: // 应用程序.js:

import React, {Component}from 'react';
import Blog from './components/Blog'
import './App.css';

class App extends Component {
  state={
    blogs:[
      {
        id: 1,
        title:'Javascript blog',
        completed: false
      },
      {
        id: 2,
        title:'Cypress blog',
        completed: false
      },
      {
        id: 3,
        title:'Testing blog',
        completed: false
      },
      {
        id: 4,
        title:'Java multi threading blog',
        completed: false
      },
      {
        id: 5,
        title:'Puppeteer blog',
        completed: false
      },
      {
        id: 6,
        title:'React Examples',
        completed: false
      }
    ]
  }
  render(){
    console.log(this.state.blogs)
    return (
      <div className="App">
      <Blog blogs={this.state.blogs}/>
      </div>
    );

  }

}

export default App;

//Blog.js //博客.js

import React, {Component}from 'react';
import BlogItem from './BlogItem';
import PropTypes from 'prop-types';


class Blog extends Component {
  render(){
    return this.props.blogs.map((blog)=>(
        <BlogItem key={blog.id} blog={blog}/>
    ));
  }

}
// PropTypes
Blog.propTypes={
    blog: PropTypes.array.isRequired
}

export default Blog;

//BlogItem.js: //BlogItem.js:

import React, { Component } from 'react'
import PropTypes from 'prop-types';

export class BlogItem extends Component {
    render() {
        return (
            <div style={blogStyle}>
                <p>{this.props.blog.title}</p>    
            </div>
        )
    }
}

// PropTypes
BlogItem.prototypes ={
    blog: PropTypes.object.isRequired
}

const blogStyle ={
    backgroundColor: '#c7c6c1'
}

export default BlogItem

// Output as of now: // Output 现在:

e

const Columns = () => 
  <div style={{ display: "grid", gridTemplateColumns: "repeat(3, 1fr)", gridGap: 20 }}>
    <div>Column 1</div>
    <div>Column 2</div>
    <div>Column 3</div>
  </div>

ReactDOM.render(<Columns />, document.getElementById("root"))

You could use bootstrap for this, you would only need to use className instead of class.您可以为此使用引导程序,您只需要使用 className 而不是 class。 I think this is what you are aiming for once you have imported Bootstrap.我认为这就是您导入 Bootstrap 后的目标。

https://getbootstrap.com/docs/4.3/layout/grid/ [1] https://getbootstrap.com/docs/4.3/layout/grid/ [1]

or yo could use CSS grid system或者你可以使用 CSS 网格系统

https://developer.mozilla.org/es/docs/Web/CSS/CSS_Grid_Layout [1] https://developer.mozilla.org/es/docs/Web/CSS/CSS_Grid_Layout [1]

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

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