React 状态 属性 插槽 ref 使用

 React 状态使用

import React,{useState} from 'react'

function Child() {
  let [count, setCount] = useState(10)
  return (
    <div style={{color: 'blue', backgroundColor: 'green',margin:'15px',padding:'15px'}}>
      <h3>子组件</h3>
      <button onClick={() => setCount(count + 1)} >子组件按钮{count}</button>
    </div>
  )
}

export default function App() {
  let [isShow, setIsShow] = useState(true)
  return(
    <div style={{color: 'red', backgroundColor: 'yellow',margin:'15px',padding:'15px'}}>
      <h3>父组件</h3>
      <button onClick={() => setIsShow(!isShow)}>父组件按钮</button>
      {isShow &&(<Child></Child>)}
    </div>
  )
}

 React props 使用

import React,{useState} from 'react'

function Child({props}) {

  console.log(props);
  return (
    <div style={{color: 'blue', backgroundColor: props.bgColor,margin:'15px',padding:'15px',display:'flex'}}>
     <div>左侧</div>
     <div style={{flex:1,textAlign:'center'}}>{props.title}</div>
     <div>右侧</div>
    </div>
  )
}

export default function App() {
  let [isShow, setIsShow] = useState(true)
  return(
    <div style={{color: 'red', backgroundColor: 'yellow',margin:'15px',padding:'15px'}}>
      <h3>父组件</h3>
      <button onClick={() => setIsShow(!isShow)}>父组件按钮</button>
      {isShow &&(<Child title="用户中心" bgColor="#ddf" ></Child>)}
      <div>页面主体内容</div>
    </div>
  )
}

 React props解构对象 使用

import React,{useState} from 'react'

function Child({title="test", bgColor="red"}) {

  return (
    <div style={{color: 'blue', backgroundColor: bgColor,margin:'15px',padding:'15px',display:'flex'}}>
     <div>左侧</div>
     <div style={{flex:1,textAlign:'center'}}>{title}</div>
     <div>右侧</div>
    </div>
  )
}

export default function App() {
  return(
    <div style={{color: 'red', backgroundColor: 'yellow',margin:'15px',padding:'15px'}}>
      <Child title="用户中心" bgColor="#ddf" ></Child>
      <div>页面主体内容</div>
      <Child title="用户中心2"  ></Child>
      <div>页面主体内容</div>
      <Child title="用户中心3" bgColor="#ddf" ></Child>
      <div>页面主体内容</div>
      <Child></Child>
      <div>页面主体内容</div>
    </div>
  )
}

React 插槽使用

import React,{useState} from 'react'

function Child(props) {

  console.log(props.children)
  return (
    <div style={{color: 'blue', backgroundColor:"yellow",margin:'15px',padding:'15px',display:'flex'}}>
     <div>左侧</div>
     <div style={{flex:1,textAlign:'center'}}>{props.children}</div>
     <div>右侧</div>
    </div>
  )
}

export default function App() {
  return(
    <div style={{color: 'red', backgroundColor: 'yellow',margin:'15px',padding:'15px'}}>
      <Child><b>test</b></Child>
      <div>页面主体内容</div>
    </div>
  )
}

React ref使用

import React,{useState,useRef} from 'react'



export default function App() {
  let unameInput=useRef();
  return(
    <div style={{color: 'red', backgroundColor: 'yellow',margin:'15px',padding:'15px'}}>
    <input type="text" ref={unameInput} placeholder="请输入内容" />
    <button onClick={()=>{
      console.log('获取输入框的值', unameInput.current.value)
    }} >按钮</button>
      <div>页面主体内容</div>
    </div>
  )
}

学习资料


【方圆】网盘资料大全
### React 进阶教程:高级用法与深入理解 #### 一、React 插槽(Portals) React插槽(Portals)是一种特殊的渲染机制,允许开发者将子节点渲染到 DOM 层次结构之外的位置。通过 `ReactDOM.createPortal` 方法,可以在不破坏现有组件层次的情况下,将某些 UI 元素放置在特定位置,比如模态框或工具提示。这种方法特别适用于需要脱离父容器样式影响的场景[^2]。 ```javascript const Modal = (props) => { return ReactDOM.createPortal( <div className="modal"> {props.children} </div>, document.getElementById('portal-root') // 渲染目标DOM节点 ); }; ``` --- #### 二、复杂的组件通信模式——Context API 随着应用程序规模的增长,传统的 props drilling 方式会变得繁琐且难以维护。React 提供了 Context API 来解决跨层级通信的需求。通过创建上下文对象并将其提供给后代组件,可以轻松实现全局状态共享[^3]。 以下是基于 Context 和 Hooks 实现的一个简单例子: ```javascript // 创建上下文 const ThemeContext = React.createContext(); // Provider 组件 function App() { const [theme, setTheme] = React.useState('light'); return ( <ThemeContext.Provider value={{ theme, setTheme }}> <Toolbar /> </ThemeContext.Provider> ); } // Consumer 组件 function Toolbar() { return ( <ThemeContext.Consumer> {({ theme, setTheme }) => ( <button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}> Toggle {theme} Mode </button> )} </ThemeContext.Consumer> ); } ``` 如果使用函数组件,则可以通过 `useContext` Hook 替代 Consumer[^4]: ```javascript function Toolbar() { const { theme, setTheme } = React.useContext(ThemeContext); return ( <button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}> Toggle {theme} Mode </button> ); } ``` --- #### 三、React 高级优化技术 为了提升性能和用户体验,React 推出了许多高级功能和技术,例如 Memoization 和 Refs。 ##### 1. **Memoization** 对于纯展示型组件,可以利用 `React.memo` 或 `shouldComponentUpdate` 减少不必要的重新渲染。这有助于提高大型列表或频繁更新区域的效率[^1]。 ```javascript const ExpensiveChild = React.memo((props) => { console.log('ExpensiveChild rendered'); return <div>{props.value}</div>; }); ``` ##### 2. **Refs** 当需要操作原生 DOM 节点或者访问子组件实例时,可以借助 `useRef` 或类组件中的 `ref` 属性来完成任务。 ```javascript const inputEl = useRef(null); const focusInput = () => { inputEl.current.focus(); }; return ( <> <input ref={inputEl} type="text" /> <button onClick={focusInput}>Focus Input</button> </> ); ``` --- #### 四、Redux 状态管理集成 在复杂应用中,推荐结合 Redux 进行集中化状态管理。通过 `react-redux` 库提供的 `Provider` 和 `connect`/`useSelector`,能够方便地连接 React 组件与 Redux Store。 ```javascript import { useSelector, useDispatch } from 'react-redux'; function Counter() { const count = useSelector(state => state.counter); // 订阅state变化 const dispatch = useDispatch(); // 获取dispatch方法 return ( <div> Count: {count} <button onClick={() => dispatch({ type: 'INCREMENT' })}> Increment </button> </div> ); } ``` --- #### 五、总结 上述内容涵盖了 React 中的一些重要进阶知识点,包括 Portals、Context API、Memoization、Refs 及 Redux 集成等。掌握这些技能可以帮助开发人员更高效地构建现代化前端应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code36

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值