1.父子组件传值需借助 中间ts文件
hook/context-manager.tsx
// 使用hook给父子组件共享数组 import { createContext } from "@tarojs/taro"; export default createContext({});
2.父组件
import TripContext from ‘../../hook/context-manager‘ import Trip_success from ‘../../components/trip_success/index‘ function Home() { const [content, setContent] = useState({}) const [contentType, setContentType] = useState(false)
return ( <View>{contentType} <TripContext.Provider value={{ content, urlName: ‘home‘,setContentType}}>
<Trip_success /> </TripContext.Provider> </View> ) }
3.子组件
import TripContext from ‘../../hook/context-manager‘import Taro, { useContext, useEffect } from "@tarojs/taro";
function Trip_success() { const { content, urlName, setContentType } = useContext(TripContext) useEffect(() => {setContentType(true)})
console.log(‘获取父组件中值‘, content, urlName) }
原文地址:https://www.cnblogs.com/gqx-html/p/12361476.html
时间: 2024-10-07 05:47:08