score:4
context api is used to share data between components in different nesting levels, you can read this from react doc:
context is primarily used when some data needs to be accessible by many components at different nesting levels. apply it sparingly because it makes component reuse more difficult.
for your case, your components image
and house
are at the same level. instead, you can use the composition and pass the props to your inner children like this:
import react, { cloneelement } from 'react';
import './style.css';
export default function app() {
const name = 'test';
return (
<parent>
<wrapper myprops={name}>
<image />
<house />
</wrapper>
</parent>
);
}
function parent({ children }) {
return children;
}
function wrapper({ children, ...props }) {
return <>{children.map((child) => cloneelement(child, { ...props }))}</>;
}
function image(props) {
console.log(props);
return <div>image</div>;
}
function house(props) {
console.log(props);
return <div>house</div>;
}
this is a simple demo: https://stackblitz.com/edit/react-szxqwg
score:0
the answer of @soufiane boutahlil is legit, but you are missing a key prop. otherwise, react will go mad at you.
function wrapper({ children, ...props }) {
return <>{children.map((child, i) =>
<react.fragment key={i}>
cloneelement(child, { ...props }))
</react.fragment>}
</>;
}
Source: stackoverflow.com
Related Query
- If I wrap a react components in one big react component will they share props
- How do I type the props for a HOC which will wrap react router's Route component
- Share the same styles between two types of component with React Styled Components
- Unsafe legacy lifecycles will not be called for components using new component APIs. React v16.3
- If I'm using the same react/redux component twice, will they share state?
- React component props not changing when they should?
- Toggle between two components /buttons using react toggle hook {Show one component and Hide another}
- React object how to wrap more than one component
- React collect all properties as one props and put it into component or NOT?
- Props are not passing from parent to child components in React Functional component
- Passing props from one class component to other in react js
- Send information from multiple children components to one parent component in React
- React component reusability :same code to wrap 2 different components
- Error Coming when passing props from one component to another and vice - versa in react using typescript
- Props becoming null after they are passed into react component
- Passing union typed props to react child component requiring one of the types in the union
- Passing props between components when one component is not a function?
- React Router Props of parent component refresh whenever I switch between children components
- Passing the props from one component to another in react
- How can I pass props from one component to another using Link and router in react
- React - Changing multiple, individual components via one other component with Context API
- How to test react components props (expect component to be called with)
- Passing entire json like objects to react component as props at one go instead of individual props
- One of my component in React is not loading while the rest of the components load fine
- objects are not valid as a react child (found object with keys) while passing props from one component to other
- Sharing React Component props with grandchildren components
- How to pass props onClick from one component to another in React JS
- How to extract props and states from one sibling component to another with react hooks?
- How can we make sure other <ListItem> components as collapsible when one <ListItem> component get expands under <List> react material ui component?
- Testing React components with Jest & Enzyme: where to test whether component props were given
More Query from same tag
- Using jQuery in React to modify CSS of an element
- Reactjs setState not working
- How to pass data between two react components having no relation
- React. How to add class in element on click?
- Difficulty importing SearchBox from 'react-google-maps'
- React - adding class to children components
- how to update the state using the redux
- use `useCallback` only work with `useReducer`?
- Show scroll only when it needs
- Material-UI InputLabel covers text when Input value set by another Input field
- Ant Design Date Picker doesn't change date on first attempt
- How to pass props to DatePicker in NextJS functional Component
- React router change url without page reload
- Reading a change in session storage from react continuously
- ReactJS use a webcomponent in JavaScript
- Persist data with localStorage - React.js
- Will ReactDOM.hydrate() trigger lifecycle methods on the client?
- Type '{ description: String; }' is missing the following properties from type 'IOrderFormWithReactProps': context, siteUrlts(2769)
- this.state.events.map is not a function
- Typescript React/ styled-components - passing function as prop to component throws error on missing type
- Removing a file from an array of files on click with react.js
- React array.splice on state array does not update dom properly?
- Populate react-select field field from firebase
- Deploying React + Express App to Kubernetes. How to structure it with docker?
- Connection to Facebook with React and Firebase
- Specific Object Save in Reducer in React Redux
- How to pass an object to onSubmit in React Hook Form
- css variable is not get applied to body tag
- Can I use CSS to appear and then disappear the border after one click?
- unable to use applyMiddleWare in redux