React實現新聞網站--詳情頁面的製作

来源:https://www.cnblogs.com/chenyingying0/archive/2020/04/16/12716169.html
-Advertisement-
Play Games

效果演示 核心代碼: src/index.js import React,{Component} from 'react'; import ReactDom from 'react-dom'; import { Layout } from 'antd'; import 'antd/dist/antd ...


效果演示

 

核心代碼:

src/index.js

import React,{Component} from 'react';
import ReactDom from 'react-dom';
import { Layout } from 'antd';
import 'antd/dist/antd.css';
import './style.css';
import AppHeader from './components/AppHeader/';
import {BrowserRouter,Route,Switch} from 'react-router-dom';
import Detail from './containers/Detail/';
import PageList from './containers/List/';

const { Header, Footer, Content } = Layout;

class App extends Component{
    render(){
        return(
            <BrowserRouter>
                <Layout style={{minWidth:1200,height:'100%'}}>
                    <Header className="header"><AppHeader/></Header>
                    <Content className="content">                       
                        <Switch>
                            <Route path="/detail/:id" component={Detail}/>
                            {/* /:id?代表參數可傳可不傳 */}
                            <Route path="/:id?" component={PageList}/>
                        </Switch>                       
                    </Content>
                    <Footer className="footer">@copyright cyy 2020</Footer>
                </Layout>
            </BrowserRouter>
        )
    }
}

ReactDom.render(<App/>,document.getElementById('root'));

 

containers/List/index.js

import React,{Component} from 'react';
import { List, Typography } from 'antd';
import axios from 'axios';
import {Link} from 'react-router-dom';
 
class PageList extends Component{
    constructor(props){
        super(props);
        this.state={
            data:[]
        }
    }

    componentWillReceiveProps(nextProps){
        const id=nextProps.match.params.id;
        axios.get("http://www.dell-lee.com/react/api/list.json?id="+id)
        .then(res=>{
            this.setState({
                data:res.data.data
            })
        })       
    }

    componentDidMount(){
        let url="http://www.dell-lee.com/react/api/list.json";
        const id=this.props.match.params.id;
        if(id){
            url=url+"?id="+id;
        }
        axios.get("http://www.dell-lee.com/react/api/list.json?id="+id)
        .then(res=>{
            this.setState({
                data:res.data.data
            })
        })
    }

    render(){
        return(
            <List
                style={{background:'#fff'}}
                bordered
                dataSource={this.state.data}
                renderItem={item => (
                    <List.Item>
                        <Link to={`/detail/${item.id}`}>
                            <Typography.Text mark>[ITEM]</Typography.Text> {item.title}
                        </Link>
                    </List.Item>
                )}
                />
        )
    }
}

export default PageList;

 

containers/Detail/index.js

import React,{Component} from 'react';
import { Card } from 'antd';
import axios from 'axios';
import './style.css';

class Detail extends Component{
    constructor(props){
        super(props);
        this.state={
            title:'',
            content:''
        }
    }

    componentDidMount(){
        const id=this.props.match.params.id;
        axios.get("http://www.dell-lee.com/react/api/detail.json?id="+id)
        .then(res=>{
            const data=res.data.data;
            this.setState(data);
        })
    }
    render(){
        console.log(this.props.match.params.id);
        return(
            <Card title={this.state.title}>
            <div class="detail" dangerouslySetInnerHTML={{__html:this.state.content}}></div>
            </Card>
        )
    }
}

export default Detail;

 

containers/Detail/style.css

.detail img{
    float:left;
    margin-right:10px;
}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 場景再現:當我使用tabLayout + Fragment 切換不同的fragment時,出現了數據重覆顯示的問題; 思考邏輯: - 每次切換fragment都會重新獲取數據,但是list集合是全局的,多切換兩次,list數據就會變多,所以需要加判斷,當list集合的數據超過第一次獲取下來list集 ...
  • 本人16年畢業於普通二本院校網路相關專業,工作經驗兩年半,目前就職業於一家普通民營企業。由於非985、211學歷硬傷,校招進大廠的門檻遠高於同屆985、211的畢業生。於是乎,從畢業到現在經歷了三家創業公司,創業公司的痛癢相信大多數才畢業的小伙伴都很清楚,沒有不坑的公司,所謂好公司就是不那麼坑的公司 ...
  •  用戶第一次插入日曆,需要授權確認:  日曆插入成功或失敗,會在快應用頂部做全局提示:  查看系統日曆:    <import name="calendar" src="../Common/ui/h-ui/advance/c_calendar"></import> <template> ...
  • 一、問題:在頂部或者底部反覆快速滑動界面,會出現自動向反方向滾動,或者視頻有聲音沒有畫面。 解決:在AndroidManifest.xml中給webview所在的activity添加硬體加速的屬性:android:hardwareAccelerated="true"。 說明:1、view控制項不能開啟 ...
  • 字元串或串(string)是由數字、字母、下劃線組成的一串字元。一它是編程語言中表示文本的數據類型。通常以串的整體作為操作對象,如:在串中查找某個子串、求取一個子串、在串的某個位置上插入一個子串以及刪除一個子串等。在微信小程式中,string字元串可用''或""聲明,可用length屬性獲取長度。常... ...
  • 一、我們填寫廣告區域 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="CSS/base.css"> <link rel=" ...
  • 寫在前面 書籍介紹:《JavaScript非同步編程》講述基本的非同步處理技巧,包括PubSub、事件模式、Promises等,通過這些技巧,可以更好的應對大型Web應用程式的複雜性,交互快速響應的代碼。理解了JavaScript的非同步模式可以讓讀者寫出結構更合理、性能更出色、維護更方便的JavaScr ...
  • 最近在學習Vue,使用到webpack的時候,出現了錯誤,可能是3和4的版本問題 webpack dev server 安裝好 後,需要在 的 增加代碼 然後此時報錯了~~~The CLI moved into a separate package: webpack cli 一大堆,一開始沒看懂,搜 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...