5 kyu
Truncate paragraph using higher-order component in React JS
536richkotze
Loading description...
React
Fundamentals
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
A useful link : https://reactjs.org/docs/higher-order-components.html#use-hocs-for-cross-cutting-concerns
Hi, @richkotze!
Thanks for a nice kata.
But, what if typeof childten isn't a string?
Hello @richkotze,
Could you help me with adding React task to Codewars?
How to add React and Jest to environment? I've tried to do that, but Codewars complaing on "import React".
You need to import react in both solution and test case code views.
Don't think Jest is available in codewars.
See the package.json for available testing frameworks: https://github.com/codewars/codewars-runner-cli/blob/master/package.json
Codewars currently doesn't allow authors to specify a test framework, but the code runner service does support other test frameworks including Jest for JS/TS. When this kata and other React kata had to be updated from Node 6.x, I had to specify the React environment available in Node 10.x/12.x using a hack because the default environment no longer includes React. The new React environment uses Jest.
@mikhama
Until Codewars supports the configuration in the kata editor, you need to use some hack using a special comment. To create a kata with React, you need to set the language version to
Node 12.x
and add the following to Preloaded:This will tell the code runner to use the React environment instead.
The tests should look something like the following:
Thank you very much!
Needs a test where
textLength
is0
.Just wondering what is the expected behaviour if textLength is 0?
Just like when
textLength
is bigger than 0: any strings with length bigger than 0 (i.e non-empty strings) are truncated, and empty strings are intact.So it should behave like -1? Don't truncate the text when it's
textLength
= 0.This comment has been hidden.
I've got a test case that expects textLength to have a default value of 10. That should cover that edge case?
This comment has been hidden.
Thanks for pointing that out. I've put in a test for that.
So I came across this test:
It seems that the test is broken? Even returning the input component directly will fail these two tests.
I can't replicate the issue, could you provide a code example of what you mean so I can try it myself please?
Even the simplest code like
will not pass those 2 tests. But this is just a HOC that doesn't do anything extra.
That example would not pass those tests.
An HOC must return a new function (this basically becomes the react component to be used)
Then to pass the
props
from the above function through to original component, you would need to spread themreturn <Parapraph {...props} />
The docs provide better examples about HOC on React site: https://reactjs.org/docs/higher-order-components.html
I... don't know. Every article about HOC that I've searched on the web (including that one) are always full of code and never any concise examples as to what HOC actually is and how they're defined and used. So it's not really helpful (at least to me).
In fact, that's my impression on the existing React katas on CW in general. The docs for such a popular framework is (surprisingly?) lacking for some unknown reason.
I think you might need to provide more useful references.
I've updated the kata description with a sentence explaining the structure and provide an example of a basic HOC to help.
Do you think this will help getting started with HOC?
I'm afraid that doesn't tell anything more than before.
Now that I've passed those two tests (by observing the result values and trying to shove random stuff in random places) and have seen other's solutions, there are clearly many more things that needs to be clarified/explained.
The remaining bits to fill in are part of the React framework.
I was thinking of creating a few React katas that help progress to this point. Link them all up.
Would that be a good approach to this?
Or I can explain the different parts in the description?
The first one is probably the best. Then you can add stuff like
If you have problems with this kata, you can try these easier parts first to get some ideas what to do: ...
Okay, I will start that soon.
I'm happy for this kata to be approved and I will add a link to the supporting kata.
I've just created an easier version of ReactJS HOC kata
Try it out: PC upgrade specs using HOC in ReactJS
Hope it helps.
Hello - this ReactJS kata on higher-order components is ready to try out.