react使用useContext实现兄弟组件通信

两个兄弟input通信,实现输入同步

无useReducer版本

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
</head>
<body>
<article id="app"></article>

<script type="text/jsx">
    let {useContext,useState} = React;

    const AppContext = React.createContext();

    const initialState = {
        inputText: '',
    };

    function Input_one() {
        const {state, setState} = useContext(AppContext);
        return (
            <input value={state.inputText} onChange={e => setState({inputText:e.target.value})}/>
        )
    }

    function Input_two() {
        const {state, setState} = useContext(AppContext);
        return (
            <input value={state.inputText} onChange={e => setState({inputText:e.target.value})}/>
        )
    }

    function App() {
        const [state,setState] = useState(initialState);
        return (
            <React.Fragment>
                <AppContext.Provider value={{state, setState}}>
                    <Input_one/>
                    <Input_two/>
                </AppContext.Provider>
            </React.Fragment>
        );
    }

    ReactDOM.render(
        <App/>,
        document.getElementById("app")
    );
</script>
</body>
</html>

有useReducer版本

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
</head>
<body>
<article id="app"></article>

<script type="text/jsx">
    let {useContext, useReducer} = React;

    const AppContext = React.createContext();

    const initialState = {
        inputText: '',
    };

    function reducer(state, action) {
        console.log('reducer',arguments);
        switch (action.type) {
            case 'UPDATE_INPUT':
                return {
                    inputText: action.data
                };
            default:
                return initialState;
        }
    }

    function Input_one() {
        const {state, dispatch} = useContext(AppContext);
        const changeInputValue = (newValue) => {
            dispatch({type: 'UPDATE_INPUT', data: newValue,});
        };

        return (
            <input value={state.inputText} onChange={e => changeInputValue(e.target.value)}/>
        )
    }

    function Input_two() {
        const {state, dispatch} = useContext(AppContext);
        const changeInputValue = (newValue) => {
            dispatch({type: 'UPDATE_INPUT', data: newValue,});
        };
        return (<input value={state.inputText} onChange={e => changeInputValue(e.target.value)}/>)
    }

    function App() {
        const [state, dispatch] = useReducer(reducer, initialState);
        return (
            <React.Fragment>
                <AppContext.Provider value={{state, dispatch}}>
                    <Input_one/>
                    <Input_two/>
                </AppContext.Provider>
            </React.Fragment>
        );
    }


    ReactDOM.render(
        <App/>,
        document.getElementById("app")
    );
</script>
</body>
</html>

参考文献:
https://itnext.io/passing-data-between-sibling-components-in-react-using-context-api-and-react-hooks-fce60f12629a

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值