constLevel5=()=>{// 接收contextconst[user,setUser]=useContext<any>(UserContext);return(<div><h5>{`${user.firstName}${user.lastName} the ${user.suffix} born`}</h5><buttononClick={()=>{setUser({ ...user,suffix: user.suffix+1})}}>Increment</button></div>)}
forwardRef
能够跟useRef一起获得子组件的DOM
在父组件创建ref,传递到子组件, 在父组件获取DOM
constFancyButton=React.forwardRef((props,ref)=>(<buttonref={ref}className="FancyButton">{props.children}</button>));// You can now get a ref directly to the DOM button:constref=React.createRef();<FancyButtonref={ref}>Click me!</FancyButton>;