Reactjs 如何在反应组件中传递道具?
我正在使用reactjs。我有一篇作文是这样写的:Reactjs 如何在反应组件中传递道具?,reactjs,Reactjs,我正在使用reactjs。我有一篇作文是这样写的: import MyComponent from './Component'; const test = [ { id: '1', Component: FirstComponent, }, { id: '2', Component: MyComponent, }, ]; return ( <Demo components={
import MyComponent from './Component';
const test = [
{
id: '1',
Component: FirstComponent,
},
{
id: '2',
Component: MyComponent,
},
];
return (
<Demo
components={test}
/>
);
从“/Component”导入MyComponent;
常数测试=[
{
id:'1',
组件:第一个组件,
},
{
id:'2',
成分:MyComponent,
},
];
返回(
);
在这种情况下,我如何为
MyComponent
传递道具?如果您需要向MyComponent
传递其他道具,那么您应该在配置中定义一个匿名组件,以便可以将新道具传递给子组件。不要忘记将道具
传递给子组件
const test = [
{
id: '1',
Component: FirstComponent,
},
{
id: '2',
Component: (props) => (
<MyComponent {...props} prop1={prop1} prop2={prop2} etc />
),
},
];
const测试=[
{
id:'1',
组件:第一个组件,
},
{
id:'2',
组件:(道具)=>(
),
},
];
如果需要向MyComponent传递额外的道具,则应在配置中定义匿名组件,以便向子组件传递新道具。不要忘记将道具
传递给子组件
const test = [
{
id: '1',
Component: FirstComponent,
},
{
id: '2',
Component: (props) => (
<MyComponent {...props} prop1={prop1} prop2={prop2} etc />
),
},
];
const测试=[
{
id:'1',
组件:第一个组件,
},
{
id:'2',
组件:(道具)=>(
),
},
];
首先我想介绍什么是道具。props意味着将jsx代码传递给函数的属性,或者我们可以说是纯函数。让我给你一个像H1标签这样的小元素的演示
我正在创建一个h1.js文件
import React from 'react';
// arrow function that get props
const H1 = (props) => {
return '<h1>{props.text}</h1>';
}
export default H1;
从“React”导入React;
//获取道具的箭头函数
常量H1=(道具)=>{
返回“{props.text}”;
}
导出默认H1;
现在,当你想使用这个功能时,只需导入和传递道具,如
#Main.js
从“React”导入React;
从“/H1.js”导入H1;
类Main扩展了React.Component{
构造函数(){
超级();
}
render(){
返回(
);
}
}
因此,您可能会了解它是如何正确工作的;) 首先我想介绍一下什么是道具。props意味着将jsx代码传递给函数的属性,或者我们可以说是纯函数。让我给你一个像H1标签这样的小元素的演示 我正在创建一个h1.js文件
import React from 'react';
// arrow function that get props
const H1 = (props) => {
return '<h1>{props.text}</h1>';
}
export default H1;
从“React”导入React;
//获取道具的箭头函数
常量H1=(道具)=>{
返回“{props.text}”;
}
导出默认H1;
现在,当你想使用这个功能时,只需导入和传递道具,如
#Main.js
从“React”导入React;
从“/H1.js”导入H1;
类Main扩展了React.Component{
构造函数(){
超级();
}
render(){
返回(
);
}
}
因此,您可能会了解它是如何正确工作的;) 你能完整地展示你的源代码吗。您的示例看起来很混乱@能否完整显示源代码。你的例子看起来很混乱@@