react 案例(无限接近真实开发)

index.js        

整体说明: 技术栈:react,react-redux, redux-thunk

 1. 新建一个文件夹 (桌面)

2. 文件夹中打开小黑窗(cmd)

3.输入命令

npx create-react-app hmtt-app
// 解释: npx create-react-app 是固定命令 hmtt-app 表示项目名称,可以修改

4. cd hmtt-app中 

                                                                 5. 下包

 6.删除不必要的文件

 7.准备启动项目

app.js


function App() {
  return (
  <div>根组件App</div>
  );
}

export default App;

        index.js

import React from 'react';
import ReactDOM from 'react-dom';

import App from './App';


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


 index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './styles/index.css'
import App from './App';
import {Provider} from 'react-redux'
import store from './store'
ReactDOM.render(<Provider store={store}><App /></Provider>, document.getElementById('root'))


 

运行命令 

npm run start

开发开始

 新建组件文件夹及所需的样式

 

 

                                                           channel.js

import React from 'react'

export default function Channel() {
  return (
    <ul className="catagtory">
      <li className="select">开发者资讯</li>
      <li>ios</li>
    </ul>
  )
}

NewsList.js

import React from 'react'
import avatar from '../assets/back.jpg'
export default function NewsList() {
  return (
    <div className="list">
      <div className="article_item">
        <h3 className="van-ellipsis">python数据预处理 :数据标准化</h3>
        <div className="img_box">
          <img src={avatar} className="w100" alt="" />
        </div>
        <div className="info_box">
          <span>13552285417</span>
          <span>0评论</span>
          <span>2018-11-29T17:02:09</span>
        </div>
      </div>
    </div>
  )
}

/store/index.js (固定写法)

import { createStore, applyMiddleware } from 'redux'
import { composeWithDevTools } from 'redux-devtools-extension'
import thunk from 'redux-thunk'  
import reducer from './reducers'

const store = createStore(reducer, composeWithDevTools(applyMiddleware(thunk)))

export default store

/store/reducers/index.js

import { combineReducers } from "redux";
import channel from './channel'
import newsList from './newsList'
export default combineReducers({
  channel,
  newsList
})

/store/reducers/channel.js 

import { CHANNEL_GET } from '../actionTypes'
const initState = {
    activeId:1, // 当前选中频道ID
    channels:[] // 导航栏的所有数据
}
const channel = (state=initState,action)=>{
    switch (action.type) {
        case CHANNEL_GET:
            
            break;
    
        default:
            return state; // 默认返回state
    }
}
export default channel

/store/reducers/newsList.js  

const initState = [];

const newsList = (state = initState, action) => {
  switch (action.type) {
    case "aaa": //先给个aaa占位
      break;

    default:
      return state; // 默认返回state
  }
};
export default newsList;

/components/channel.js

 /store/actions/channel.js

import axios from "axios"; // 引入axios
import { CHANNEL_GET } from "../actionTypes";
export const getChannel = () => { 
  const fn = async (dispatch) => {
    const res = await axios.get("http://geek.itheima.net/v1_0/channels");
    console.log("导航栏数据", res);
    dispatch({ type: CHANNEL_GET, payload: res.data.data.channels });
  };
  return fn;
};
// 在getChannel函数中 创建一个函数fn ,在fn函数中发请求获取数据
// 调用dispatch()设置type和接收数据
// 返回fn

 /store/actionTypes     (固定写法)

export const CHANNEL_GET = 'channel/get'

 /store/reducers/channel.js

 

 此时已经获取到数据了 接下来就是使用useSelect渲染页面了

 

                         

 

 

  • 3
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
React Refs 是 React 提供的一种访问 DOM 元素或组件实例的方法。通过 Refs,我们可以在组件中直接访问到被 Refs 引用的 DOM 元素或组件实例,从而进行一些特殊的操作。 下面,我来举一个 React Refs 的案例: 假设我们有一个 Input 组件,需要在组件加载完毕后自动获取焦点,我们可以通过 Refs 来实现这个功能。具体的实现步骤如下: 1. 在 Input 组件中定义 Refs: ```jsx class Input extends React.Component { constructor(props) { super(props); this.inputRef = React.createRef(); // 创建 Refs } render() { return <input type="text" ref={this.inputRef} />; } } ``` 2. 在 componentDidMount 生命周期中,使用 Refs 获取到 input 元素,并调用 focus() 方法: ```jsx class Input extends React.Component { constructor(props) { super(props); this.inputRef = React.createRef(); // 创建 Refs } componentDidMount() { this.inputRef.current.focus(); // 获取 input 元素并获取焦点 } render() { return <input type="text" ref={this.inputRef} />; } } ``` 这样,当 Input 组件加载完毕后,它的 input 元素就会自动获取焦点了。 注意,Refs 只能在类组件中使用,不能在函数式组件中使用。另外,Refs 的值可以是回调函数,而不仅仅是 React.createRef() 方法返回的对象。如果使用回调函数的方式,可以在回调函数中访问到组件实例或 DOM 元素,例如: ```jsx class Input extends React.Component { constructor(props) { super(props); this.inputRef = null; // 创建 Refs this.setInputRef = element => { this.inputRef = element; }; } componentDidMount() { this.inputRef.focus(); // 获取 input 元素并获取焦点 } render() { return <input type="text" ref={this.setInputRef} />; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值