React跨函数组件修改状态实现及React Redux简单入门求助
方案1:状态提升(无需引入额外库,保持现有组件结构)
你的两个组件是同级兄弟组件,没有直接的父子依赖,把共享的输入状态提到共同父组件App中统一管理即可,完全保留你原有的组件层级和渲染结构:
- App组件新增state存储共享的输入值,同时封装修改状态的方法
- 将修改方法通过props传给Component1,输入变化时调用父组件方法更新公共状态
- 将公共状态值通过props传给Component2,直接绑定到它的输入框上即可
修改后的完整代码如下:
import "./App.css"; import React, { Component } from "react"; function Component1(props) { const handleChange = (e) => { props.onValueChange(e.target.value); }; return ( <div style={{ background: "#f9f9f9" }}> <label>Component 1: </label> <input className={props.class} placeholder={props.placeholder} value={props.value} onChange={handleChange} /> </div> ); } const Component2 = (props) => { return ( <div style={{ background: "#f5f5f5", marginTop: "10px" }}> <label>Component 2: </label> <input className={props.class} placeholder={props.placeholder} value={props.value} // 如果需要Component2修改内容也同步到Component1,就加下面这行,不需要可以删除 onChange={(e) => props.onValueChange(e.target.value)} /> </div> ); } class App extends Component { state = { sharedValue: "" } handleValueChange = (newVal) => { this.setState({ sharedValue: newVal }) } render() { return ( <div className="App" style={{ display: "flex", flexDirection: "column" }}> <Component1 placeholder="Enter Some Value" class="sibling" value={this.state.sharedValue} onValueChange={this.handleValueChange} /> <Component2 placeholder="Updating Value" class="sibling" value={this.state.sharedValue} onValueChange={this.handleValueChange} /> </div> ); } } export default App;
方案2:React Redux实现(适合多组件共享状态的复杂场景)
如果后续项目有更多跨组件共享状态的需求,可以用Redux实现,以下是最简适配版本:
- 先安装依赖:
npm install @reduxjs/toolkit react-redux - 新建状态切片文件
src/store/inputSlice.js:
import { createSlice } from '@reduxjs/toolkit' const initialState = { value: '' } const inputSlice = createSlice({ name: 'input', initialState, reducers: { updateValue: (state, action) => { state.value = action.payload } } }) export const { updateValue } = inputSlice.actions export default inputSlice.reducer
- 新建全局store文件
src/store/index.js:
import { configureStore } from '@reduxjs/toolkit' import inputReducer from './inputSlice' export const store = configureStore({ reducer: { input: inputReducer } })
- 修改入口文件
src/index.js,用Provider包裹App组件注入store:
import React from 'react' import ReactDOM from 'react-dom/client' import App from './App' import { Provider } from 'react-redux' import { store } from './store' const root = ReactDOM.createRoot(document.getElementById('root')) root.render( <Provider store={store}> <App /> </Provider> )
- 最后修改你的App.js组件,用Redux提供的hooks读写状态:
import "./App.css"; import React, { Component } from "react"; import { useSelector, useDispatch } from 'react-redux' import { updateValue } from './store/inputSlice' function Component1(props) { const dispatch = useDispatch() const value = useSelector(state => state.input.value) const handleChange = (e) => { dispatch(updateValue(e.target.value)) }; return ( <div style={{ background: "#f9f9f9" }}> <label>Component 1: </label> <input className={props.class} placeholder={props.placeholder} value={value} onChange={handleChange} /> </div> ); } const Component2 = (props) => { const value = useSelector(state => state.input.value) const dispatch = useDispatch() return ( <div style={{ background: "#f5f5f5", marginTop: "10px" }}> <label>Component 2: </label> <input className={props.class} placeholder={props.placeholder} value={value} onChange={(e) => dispatch(updateValue(e.target.value))} /> </div> ); } class App extends Component { render() { return ( <div className="App" style={{ display: "flex", flexDirection: "column" }}> <Component1 placeholder="Enter Some Value" class="sibling" /> <Component2 placeholder="Updating Value" class="sibling" /> </div> ); } } export default App;
Redux核心逻辑是把所有共享状态放到全局store统一管理,任何组件都可以通过useSelector读取状态,通过dispatch触发action修改状态,所有依赖该状态的组件都会自动更新,适合跨层级、多组件共享状态的复杂项目场景。
内容的提问来源于stack exchange,提问作者Abdul Rasheed Azar
相关产品推荐
相关产品推荐

