'Why do you use reactjs children props?
App.js
import Header from "./header";
export default function App() {
return (
<div className="App">
<Header />
</div>
);
}
Header.js
const Header = () => {
return (
<div>
<h1>Hello World</h1>
</div>
);
};
export default Header;
The code that was applied using "children props"
App.js
import Header from "./header";
export default function App() {
return (
<div className="App">
<Header>
<h1>Hello World</h1>
</Header>
</div>
);
}
Header.js
const Header = ({children}) => {
return (
<div>
{children}
</div>
);
};
export default Header;
I think I can just make it using 'props', so what is the reason for using 'children props'?
Solution 1:[1]
We sometimes use props or children to pass in values of strings, integers, boolean or etc. This helps to reduce the number of lines in one file and to make your code more readable.
The most important element is that you can reuse this component in your project. Reusability components save you time and extra lines of code.
I usually create reusable components for buttons, layouts, displays, Image Slider and etc. Below is the sample taken from you, but I've added children as props to accept any children's attributes.
const Header = ({children}) => {
return (
<div>
{children}
</div>
);
};
export default Header;
Solution 2:[2]
props.children is a special prop, automatically passed to every component, that can be used to render the content included between the opening and closing tags when invoking a component. You can code it like this
const Header = (props) => {
return <div>{props.children}</div>;
};
or
const Header = ({children}) => {
return <div>{children}</div>;
};
This might be helpful: https://codeburst.io/a-complete-guide-to-props-children-in-react-c315fab74e7c
Solution 3:[3]
This pattern of HTML is very familiar to us.
<select>
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
</select>
The thing is, this pattern shows the hierarchy of our components. In the same way in React, you can expose your component's hierarchy by children.
const Header = ({children}) => {
return (
<div>
{children}
</div>
);
};
export default Header;
You can paint the same HTML with props of course, but this way is more declarative. As you can assume that anything wrapped by Header component will treat as children.
One more advantage of using children is that it can give you more reusability. You can find more information about composition in the official document.
Solution 4:[4]
Now the code works fine until you use an InputData that does not contain a "*"
Not plausible. As others have commented, the second argument to strchr() is an int, by which you directly pass the value of the char you want to search for. You are instead passing a pointer to an object containing that value, i.e. trying to pass the value indirectly, and that will not work as intended unless by complete accident. It is possible, however, that instead of crashing, such a program simply produces the wrong result.
But let's suppose for the sake of argument that the real code were actually correct in that regard:
char *magicchar;
int IndexofMagicchar = 0;
magicchar = strchr(InputData, '*');
IndexofMagicchar = (int) (magicchar - InputData);
That code fragment is still flawed, because strchr returns a null pointer in the event that it does not find the specified character, and pointer difference is not defined when either operand is a null pointer. It is in fact defined only when both operands point into or just past the end of the same array. The program crashing is one of the best possible manifestations of the UB resulting from computing that difference. I would be inclined to guess, however, that it is not actually computing the difference that crashes, but rather some later use of the value of IndexofMagicchar.
The fix was simply to add a simple test on the magicchar variable :
char *magicchar; int IndexofMagicchar =0; magicchar=strchr(InputData,"*"); if (magicchar!=NULL) IndexofMagicchar = (int)(magicchar - InputData);
And that's an appropriate solution, modulo the wrong-quotes problem. If magicchar is computed as a null pointer then you should not use it as a pointer difference operand. It also leaves IndexofMagicchar with a value that is a valid index into any string, which is probably the key effect with respect to avoiding a crash.
Note, however, that it still risks the program doing the wrong thing later on, because if strchr returns a null pointer then the resulting value of IndexOfMagicchar is not the index of an appearance of '*'. This is perhaps a robustness issue -- perhaps it manifests only if the program receives malformed input, for example. That's the kind of thing from which security vulnerabilities are made, though the risk profile for your particular program is probably small.
My question is that the code was working fine even if the Input does not contain a "*" why the crash become systematic ?
I do not accept that the original code was "working fine". It might not have been crashing, but that's a different thing. Undefined behavior can manifest as an appearance of working fine, but anything can happen, for any reason or no apparent reason.
As a practical matter, I would be inclined to guess that the original bad code was causing the program to perform an out-of-bounds array access that nevertheless happened to hit accessible memory, and a change elsewhere in the program, or in the compile options, or compiler, or runtime context caused those OOB accesses to start hitting inaccessible memory instead. The details are immaterial however: the code was wrong, and needed to be fixed. The second version presented still needs to be fixed, even if it is not crashing.
Solution 5:[5]
- Use proper types (not
int) - Write safe
strnchrfunction - Second parameter of the
strchrischarnotchar *. String literals decay tochar *pointers
char *mystrnchr(const char *restrict str, size_t size, int ch)
{
char *result = NULL;
while(size && *str != ch && *str) {str++;size--;}
if(size && *str) result = (char *)str;
return result;
}
/* somewhere in another function */
char *magicchar;
ptrdiff_t IndexofMagicchar =0;
magicchar=mystrnchr(InputData, size_of_InputData, '*');
if(magicchar)
IndexofMagicchar = magicchar - InputData;
else {/* handle not found*/ }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Zeeshan |
| Solution 2 | |
| Solution 3 | Jae |
| Solution 4 | John Bollinger |
| Solution 5 |
