score:3
Accepted answer
The best way I know how is to use the React.Children utility. Here's a quick Codepen to see the concept in action. http://codepen.io/anon/pen/RrKbjZ
So the code in the render()
function of the App
component in your example would look something like this.
render: function() {
var regButton = (this.state.loggedIn) ? null : (<Link className="Navigation__link" to="register">Register</Link>);
var accountButton = (this.state.loggedIn) ? (<Link className="Navigation__link" to="account">My Account</Link>) : null;
var childrenWithProps = React.Children.map(this.props.children, (Child, i) => {
return React.cloneElement(Child, {
loggedIn: this.state.loggedIn
});
});
return (
<div>
<nav className="Navigation">
{
this.state.loggedIn ?
(<Link className="Navigation__link" to="/logout">Log out</Link>) :
(<Link className="Navigation__link" to="/login">Sign in</Link>)
}
{regButton}
{accountButton}
<Link className="Navigation__link" to="dashboard">Dashboard</Link>
</nav>
<div>
{childrenWithProps}
</div>
</div>
);
}
score:0
I have the same problem before. This issue may help you.
https://github.com/rackt/react-router/issues/1857
However, this method for passing parameter is not recommend, especially when you are building a big app.
I recommend some state manager for js, such as "Redux".
Source: stackoverflow.com
Related Query
- How do you pass top level component state down to Routes using react-router?
- I can not get the state from react router Link component using useLocation. So how can I pass it?
- How to use Context API to pass down a state while using a Router in React JS
- How to pass the match when using render in Route component from react router (v4)
- How do you pass data when using the navigate function in react router v6
- How do I stop state store data from accumulating in a redux component every time I navigate to it using react router
- How to pass a prop from one component to another when using react router link
- How do you pass an image from the root component to a child component using state variables in React?
- How can I pass props from one component to another using Link and router in react
- React Router with Redux: Pass only necessary props down to child level routes
- How to pass a state to another state in same component using functional component in React
- How do you pass data out of a React component using a callback?
- How to set initial state in top level react component with redux?
- How to build and add a child component in React using React Top Level Api's
- How to pass Children component state to Parent component state in React using Hooks
- React - how to pass state to another component
- React router and this.props.children - how to pass state to this.props.children
- Can you pass down state to child components with React Hooks?
- How to use React Router on top of Rails router (not using react-rails gem)?
- How to pass a state className variable to another component in react
- How to pass params into link using React router v6?
- How can I pass data using <Redirect> in react router v4?
- React router v4 - How do you access the state that is passed via Redirect?
- using css modules in react how can I pass a className as a prop to a component
- How to test React state after calling a component method that updates state - using Enzyme
- how to pass props to the route when using react router <Link>
- How do I pass all state variables inside react hooks to my child component
- React: How to add onChange functionality inside of Function component using React Hooks? Need onClick event from a checkbox to influence input state
- How to share application State between React Router v4 routes
- How to pass state to another component using useHistory?
More Query from same tag
- How to show single model when onClick in React?
- How to get first child to work with JSS-Nested
- Stateful Component with Typescript and react-redux: Argument of type 'typeof MyClass' is not assignable to parameter of type Component
- React - Conditional rendering is not working well
- Exactly how are you supposed to debug JSX in React.js?
- React useEffect and React-Query useQuery issue?
- How to clear/destroy Chart.js canvas when the chart is represented as a react JSX element
- call a same instance component multiple times ReactJS
- Return initial state after state changes
- How do i deconstruct a javascript user object to display on an account page?
- Console log output of element in a React app
- Parsing error: Unexpected token, expected ","
- How to change the width of tooltip container in reactjs?
- Change value of object property with checkbox React
- API Call Not Returning Information
- How to access parent array variable/prop from child array in React?
- Nested Array in React Object
- React-router not redirecting to page
- React using TypeScript, Don't use {} as a type. {
- Why can't I use the className attribute for some React.Components?
- How to use enum (attached to a class as a static variable) as a type
- How can I import local font in Reactjs with Styled Components?
- Trying to turn an array into a list
- Getting the state with rematch
- use context value as initial state value - react hooks
- React toggle button after mapping through list
- How to add typescript paths to storybook
- Pass Values from JSON as Props to the child component
- Expo XDE cannot run application throws Tunnel packager not found
- Node Sass version 7.0.1 is incompatible with ^4.0.0