EN
React - how to set child component text between tags from parent component using props?
1 answers
0 points
How can I set child component text between tags from a parent component using props?
Let's say I have a component:
xxxxxxxxxx
1
const MyComponent = () => {
2
return (
3
<div>some text</div>
4
);
5
};
Now, what I want to do is to set "some text
" between <div>
tags from another component.
Can you help me with that?
1 answer
0 points
You can use props.children
to set the child component text from parent using props.
Practical example
In this example App
is a parent component and MyComponent
is a child.
xxxxxxxxxx
1
//Note: Uncomment import lines during working with JSX Compiler.
2
// import React from 'react';
3
// import ReactDOM from 'react-dom';
4
5
const MyComponent = (props) => {
6
return (
7
<div>{props.children}</div>
8
);
9
};
10
11
const App = () => {
12
return (
13
<div>
14
<MyComponent>some text here...</MyComponent>
15
</div>
16
);
17
};
18
19
const root = document.querySelector('#root');
20
ReactDOM.render(<App />, root);
References
0 commentsShow commentsAdd comment