Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

javascript - Is there anyway to inject components into bigger react components?

Let's say I have a Post component. This component is used heavily across my code. And thus it needs to handle the onDelete and onEdit inside it so that I don't have to repeat the code again and again.

But it needs to be tested. So I need to mock onDelete and onEdit.

This is achievable by writing the component in a file, and connecting it with the backend in another.

post/
  post.tsx      # just the UI
  index.tsx     # post.tsx but connected with the store or backend

and in tests, I can import post.tsx and mock the handlers.


Now let's say I have a PostList component that also needs to be tested. However this PostList imports Post from index.tsx which is connected to the real handlers. And thus I lost the ability to mock the handlers for Post.

Is there anyway to inject Post into PostList like how we do in OOP?

question from:https://stackoverflow.com/questions/65921175/is-there-anyway-to-inject-components-into-bigger-react-components

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...