I am new to React and I'm having an issue with passing custom props to this.props.children
. I have tried React.cloneElement
and I can see the prop when I do a console.log in the class I created it but it gets lost when I try to past it. I don't know if maybe React-Router-Dom
is doing something to cause the prop to not get passed. any help would be greatly appreciated.
I started this project using Create-React-App
from GitHub. I have the following software installed.
"react": "^16.3.2",
"react-dom": "^16.3.2",
"react-redux": "^5.0.6",
"react-router-dom": "^4.2.2",
"react-scripts": "1.1.4",
"redux": "^3.7.2",
in App.js
render() {
return (
<Provider store={store}>
<BrowserRouter>
<Switch>
<Route exact path="/" component={LoginPage} />
<Route path="/Login" component={LoginPage} />
<RootPage>
<Route path="/TestPage" component={testPage} />
</RootPage>
</Switch>
</BrowserRouter>
</Provider>
);
}
in RootPage.js (Parent)
render() {
const { classes, theme, children } = this.props;
var childrenWithProps = React.Children.map(children, child => React.cloneElement(child, { doSomething: "Test String" }));
console.log(childrenWithProps)
return (
<div>
{childrenWithProps}
</div>
);
}
With the console.log
above in RootPage.js
I can see the prop "doSomething".
in TestPage.js (Child)
render(){
const { classes, theme } = this.props;
console.log(this.props)
return (
<div>
</div>
);
}
in the above console.log
in TestPage.js I dont see "doSomething" prop I passed.
Thank you for any help.