score:1
One of possible solutions is to use some kind of message bus or pubSub. In that kind of approach, Bar component would have to subscribe to the bus, and show/hide itself. Foo component would publish appriopriate messages in componentWillReceiveProps. More: here
score:2
One simple solution is to use Bar component
inside of the Foo component
and render that based on a ternary condition
.
Also return a JSX element(Valid react element from Bar component) rather than a string.
const Example = () => {
return (
<Foo bar={true}>
<div>some div</div>
</Foo>
)
};
class Foo extends React.Component {
render () {
return (
<div>
<div>{this.props.children}</div>
{this.props.bar? <Bar />: null}
</div>
)
}
};
const Bar = () => <div>I am some bar</div>;
ReactDOM.render(<Example/>, document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.js"></script>
<div id="app"></div>
Source: stackoverflow.com
Related Query
- How can I remove a component using the children prop in React
- I'm using react-slick, how can I export a React Component as a single element but remove the parent div on render
- How can remove console.log in the production build of a React application created using create-react-app?
- using css modules in react how can I pass a className as a prop to a component
- I can not get the state from react router Link component using useLocation. So how can I pass it?
- How can I update component in React after adding a new item into the database, using react hooks?
- How can I add or remove class using querySelectorAll() in react functional component
- How can I pass a function as a prop to a React component using react-router <Redirect />?
- How to use the useHook in the component rather than passing the value returned from it as a prop to the components using react and typescript?
- How can I send a new prop to a component after time using the setTimeout() function?
- How can I pass the children of a function component to another function component in a react class?
- how can my call the action in my component using typescript with react redux?
- How can a unique instance of a React component respond to the result of an async action using Flux?
- How can I convert the this cascading dropdown class component to functional one using React Hooks?
- How can I update State in React using the React-Select Component and User Selection?
- react (class based) : How can I update JSON object in parent component using setState after the object being passed in function from child to parent?
- How can I use different properties for the same component using Styled Component for React JS?
- How to evaluate the passed prop to the styled component using react and typescript?
- How can I get the ref or info of the React component that is scrolled into view during a vertical scroll using React Scrollbars?
- How do I restrict the type of React Children in TypeScript, using the newly added support in TypeScript 2.3?
- ReactJS - How can I access the displayName of a component using javascript?
- How to make the whole Card component clickable in Material UI using React JS?
- How can I fix jsx-a11y/anchor-is-valid when using the Link component in React?
- How can I write an else if structure using React (JSX) - the ternary is not expressive enough
- How can I remove unused imports/declarations from the entire project of React Typescript?
- How to pass the match when using render in Route component from react router (v4)
- How can I define the type for a <video> reference in React using Typescript?
- How to use React.FC<props> type when the children can either be a React node or a function
- How can I spread props to a React component that uses exact props when using Flow?
- How can I pass CSS classes to React children and ensure they will override the child’s local class?
More Query from same tag
- typescript access object dynamically
- Parsing JSON into an array of objects for React state hook
- How to get a component to be fixed at the bottom until the user scrolls to the footer with CSS in React
- How can I store cookies on a local client that are returned from a local backend API?
- Unable to move handle on a react slider using cypress
- What happened with the render() after React Hooks?
- Add Items to cart without the user logged in react.js and redux
- Warning: Unknown event handler property `onValueChange`. It will be ignored
- What am I doing wrong? - Uncaught TypeError: Object(...) is not a function - React GraphQL
- TypeError: Cannot read properties of undefined (reading 'target') (Typscript/React)
- react-select dropdown opens inside modal
- Not able to change the value of an element using react
- I want to display animated text using <ScrollAnimation> but i see a blank page
- How to delete in axios obj in to do list?
- can we add virtual scrolling on React Kendo Grid Wrapper
- React doesn't rerender on props change
- How do I automatically pass updated props in mounted ReactJS component
- TypeError: author is undefined
- How to handle asynchronous calls to setState in React?
- How to extract the clicked value in the drop down menu using React Material UI?
- How to pass multiple functional components in react route?
- Redux: Waiting for state changes before doing something else
- Assignment of value in ComponentDidMount instead of constructor function in React
- React Hooks - Change select options when I select an option on another select
- Show submitted text in both divs with React
- How to show list of data from GET-request
- How can I invoke a callback within a React function component?
- How to get selected value on Dropdown component in office-ui-fabric-react?
- Managing State when Passing React Component Constuctor Function to Another Component as Attribute
- How to make bundel.js execute code right on home page?