1. 在函数组件实现跨组件通信的方式
2. 注册Context将value传递给子组件
let MyContext = React.createContext('默认值');
<MyContext.Provider value={} >
let value = useContext(MyContext)
文章来源地址https://www.toymoban.com/news/detail-666027.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style></style>
<script src="../react.development.js"></script>
<script src="../react-dom.development.js"></script>
<script src="../babel.min.js"></script>
<script src="../lodash.min.js"></script>
</head>
<body>
<div id="app"></div>
<script type="text/babel">
let app = document.querySelector('#app');
let root = ReactDOM.createRoot(app);
let { useContext } = React;
let MyContext = React.createContext('默认值');
// 实现三层组件的跨组件通信
let Welcome = (props) => {
return (
<div>
hello Welcome
<MyContext.Provider value="welcome的问候~~~">
<Head />
</MyContext.Provider>
</div>
);
}
let Head = () => {
return (
<div>
hello Head
<Title />
</div>
);
}
let Title = () => {
let value = useContext(MyContext);
return (
<div>
hello Title, { value }
</div>
);
}
let element = (
<Welcome />
);
root.render(element);
</script>
</body>
</html>
文章来源:https://www.toymoban.com/news/detail-666027.html
到了这里,关于4.react useContext使用与常见问题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!