A few things here:
- You need to tell typescript what props to expect for the
functionalComp. You do this by passing the props type toReact.FClikeReact.FC<PropsTypeHere>. That makes your props definition look like this:
export interface DemoFCProps {
itemArray: FCProps[],
functionalComp: React.FC<FCProps>
}
- You should let react render this for your, rather than calling the function component directly. That makes using
<MyComponent {...props} />syntax.
export default function DisplayFCs(props : DemoFCProps) {
const FunctionalComp = props.functionalComp
return (
<div>
{
props.itemArray.map(
(item) => <FunctionalComp {...item} />
)
}
</div>
)
}
Playground
But this isn't all the way to your goal:
The hope was that this would allow me to call the DisplayFCs component with any component and array of props.
To do that, DisplayFCs needs to be generic. Let's start with it's props.
export interface DisplayFCsProps<
FCProps extends { [key: string]: unknown }
> {
itemArray: FCProps[],
functionalComp: React.FC<FCProps>
}
Here FCProps is a generic type parameter. Whatever that type is, this constructs a type where it requires an array of that type, and a functional component that takes that type as props.
Then you need to use that props type from a generic functional component:
export default function DisplayFCs<
Props extends { [key: string]: unknown }
>(props: DisplayFCsProps<Props>) {
const FunctionalComp = props.functionalComp
return (
<div>
{
props.itemArray.map(
(item) => <FunctionalComp {...item} />
)
}
</div>
)
}
Which now works as you would expect
function Person(props: { name: string, age: number }) {
return <></>
}
const testPersons = <DisplayFCs
functionalComp={Person}
itemArray={[
{ name: 'Sue', age: 30 },
{ name: 'joe', age: 24 }
]}
/>
function Book(props: { title: string, author: string }) {
return <></>
}
const testBooks = <DisplayFCs
functionalComp={Book}
itemArray={[
{ title: 'Necronomicon', author: 'Sue' },
{ title: 'The Expanse', author: 'Joe' }
]}
/>
Playground
Answer from Alex Wayne on Stack Overflowreactjs - Passing Props to a Functional Component that was also passed as a Param - Stack Overflow
reactjs - Do function components have props in React? - Stack Overflow
React: Passing props to function components
Use Default Props of imported component in functional component
Videos
A few things here:
- You need to tell typescript what props to expect for the
functionalComp. You do this by passing the props type toReact.FClikeReact.FC<PropsTypeHere>. That makes your props definition look like this:
export interface DemoFCProps {
itemArray: FCProps[],
functionalComp: React.FC<FCProps>
}
- You should let react render this for your, rather than calling the function component directly. That makes using
<MyComponent {...props} />syntax.
export default function DisplayFCs(props : DemoFCProps) {
const FunctionalComp = props.functionalComp
return (
<div>
{
props.itemArray.map(
(item) => <FunctionalComp {...item} />
)
}
</div>
)
}
Playground
But this isn't all the way to your goal:
The hope was that this would allow me to call the DisplayFCs component with any component and array of props.
To do that, DisplayFCs needs to be generic. Let's start with it's props.
export interface DisplayFCsProps<
FCProps extends { [key: string]: unknown }
> {
itemArray: FCProps[],
functionalComp: React.FC<FCProps>
}
Here FCProps is a generic type parameter. Whatever that type is, this constructs a type where it requires an array of that type, and a functional component that takes that type as props.
Then you need to use that props type from a generic functional component:
export default function DisplayFCs<
Props extends { [key: string]: unknown }
>(props: DisplayFCsProps<Props>) {
const FunctionalComp = props.functionalComp
return (
<div>
{
props.itemArray.map(
(item) => <FunctionalComp {...item} />
)
}
</div>
)
}
Which now works as you would expect
function Person(props: { name: string, age: number }) {
return <></>
}
const testPersons = <DisplayFCs
functionalComp={Person}
itemArray={[
{ name: 'Sue', age: 30 },
{ name: 'joe', age: 24 }
]}
/>
function Book(props: { title: string, author: string }) {
return <></>
}
const testBooks = <DisplayFCs
functionalComp={Book}
itemArray={[
{ title: 'Necronomicon', author: 'Sue' },
{ title: 'The Expanse', author: 'Joe' }
]}
/>
Playground
I could be wrong but I guess you just need to change your DisplayFCs's map:
{
// for each item in the array
props.itemArray.map(
// spread that item's properties as props to the functional component
(item)=>(<props.functionalComp {...item} />)
)
}
Yes. You pass the props inside the function component directly (using props) like this:
function myFunc(props){
console.log(props.content);
return <div>{props.content}</div>;
}
assuming that the props that passes in has element content.
To clarify, there is no this in function components because they're not classes at all! Instead, you pass in props directly into the function.
In fact, it is probably the one of the first things that you learn in React. So if you're new, then I suggest the tutorial here.
Yes, like this,
the props are passed to the function component as the first argument
export default function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
here read this article it explains everything: https://medium.com/@PhilipAndrews/react-how-to-access-props-in-a-functional-component-6bd4200b9e0b
You would need to pass down each prop individually for each function that you needed to call
<CreateProfile
onFirstNameChange={this.firstNameChange}
onHide={close}
show={this.state.showModal}
/>
and then in the CreateProfile component you can either do
const CreateProfile = ({onFirstNameChange, onHide, show }) => {...}
with destructuring it will assign the matching property names/values to the passed in variables. The names just have to match with the properties
or just do
const CreateProfile = (props) => {...}
and in each place call props.onHide or whatever prop you are trying to access.
I'm using react function component
In parent component first pass the props like below shown
import React, { useState } from 'react';
import './App.css';
import Todo from './components/Todo'
function App() {
const [todos, setTodos] = useState([
{
id: 1,
title: 'This is first list'
},
{
id: 2,
title: 'This is second list'
},
{
id: 3,
title: 'This is third list'
},
]);
return (
<div className="App">
<h1></h1>
<Todo todos={todos}/> //This is how i'm passing props in parent component
</div>
);
}
export default App;
Then use the props in child component like below shown
function Todo(props) {
return (
<div>
{props.todos.map(todo => { // using props in child component and looping
return (
<h1>{todo.title}</h1>
)
})}
</div>
);
}