Arrays React-props语法-无法将引用传递到数组
(编辑) 在下面的简单示例中,我对传递和访问引用数组成员的道具的语法非常熟悉 当作为道具传递时,我如何正确处理对象数组的属性 我想通过参考传递,但我不明白为什么我不能这样做 谢谢Arrays React-props语法-无法将引用传递到数组,arrays,reactjs,Arrays,Reactjs,(编辑) 在下面的简单示例中,我对传递和访问引用数组成员的道具的语法非常熟悉 当作为道具传递时,我如何正确处理对象数组的属性 我想通过参考传递,但我不明白为什么我不能这样做 谢谢 // Define a type constant const MyType { constructor( props ) { this.state = { panelNumber : 0, panelDisplayMode : 'SINGLE' }
// Define a type constant
const MyType {
constructor( props ) {
this.state = {
panelNumber : 0,
panelDisplayMode : 'SINGLE'
}
}
}
// create a collection of those type constants
class MyStore extends React.Component{
MyCollection = [];
constructor () {
super();
}
componentWillMount(){
var nonsense = new MyType;
nonsense.panelNumber = 123;
nonsense.panelDisplayMode = "SINGLE";
this.MyCollection.push(nonsense);
nonsense = new MyType;
nonsense.panelNumber = 456;
nonsense.panelDisplayMode = "DOUBLE";
this.MyCollection.push(nonsense);
nonsense = new MyType;
nonsense.panelNumber = 789;
nonsense.panelDisplayMode = "TRIPLE";
this.MyCollection.push(nonsense);
}
// pass one of the instances (array) of the collection to a component.
render () {
<div>
<MyComponent panel={this.MyCollection[2]}
</div?
}
}
// access one of the properties of the instance (array).
const MyComponent = props => (
<div>
<p>MODE:</p>{ props.panel.panelDisplayMode }
</div>
);
//定义一个类型常量
常量MyType{
构造器(道具){
此.state={
编号:0,
panelDisplayMode:“单一”
}
}
}
//创建这些类型常量的集合
类MyStore扩展了React.Component{
MyCollection=[];
构造函数(){
超级();
}
组件willmount(){
var=新的MyType;
无意义。面板编号=123;
displaymode=“SINGLE”;
这个。我收集。推(胡说);
无意义=新MyType;
无意义。面板编号=456;
无意义。panelDisplayMode=“DOUBLE”;
这个。我收集。推(胡说);
无意义=新MyType;
无意义。面板编号=789;
无意义。面板显示模式=“三重”;
这个。我收集。推(胡说);
}
//将集合的一个实例(数组)传递给组件。
渲染(){
使用setState
函数更新状态,并映射集合数组以将单个集合传递给组件
class MyStore extends React.Component{
constructor(props) {
this.state = {
MyCollection: [],
}
}
componentWillMount(){
var myCollection = [];
var nonsense = new MyType;
nonsense.panelNumber = 123;
nonsense.panelDisplayMode = "SINGLE";
myCollection.push(MyPanel);
nonsense = new MyType;
nonsense.panelNumber = 456;
nonsense.panelDisplayMode = "DOUBLE";
myCollection.push(MyPanel);
nonsense = new MyType;
nonsense.panelNumber = 789;
nonsense.panelDisplayMode = "TRIPLE";
myCollection.push(MyPanel);
this.setState({MyCollection:myCollection});
}
// pass one of the instances (array) of the collection to a component.
render () {
<div>
{this.state.MyCollection.map(
singleCollection=>{
<MyComponent panel={singleCollection}>
}
)}
</div>
}
}
// access one of the properties of the instance (array).
const MyComponent = props => (
<div>
<p>MODE:</p>{ props.panel.panelDisplayMode }
</div>
);
类MyStore扩展React.Component{
建造师(道具){
此.state={
MyCollection:[],
}
}
组件willmount(){
var myCollection=[];
var=新的MyType;
无意义。面板编号=123;
displaymode=“SINGLE”;
myCollection.push(MyPanel);
无意义=新MyType;
无意义。面板编号=456;
无意义。panelDisplayMode=“DOUBLE”;
myCollection.push(MyPanel);
无意义=新MyType;
无意义。面板编号=789;
无意义。面板显示模式=“三重”;
myCollection.push(MyPanel);
this.setState({MyCollection:MyCollection});
}
//将集合的一个实例(数组)传递给组件。
渲染(){
{this.state.MyCollection.map(
singleCollection=>{
}
)}
}
}
//访问实例(数组)的一个属性。
常量MyComponent=props=>(
模式:{props.panel.panelDisplayMode}
);
编辑
我在修复了一些语法错误后运行了这段代码。你有什么具体的错误吗?顺便说一下,这是代码
class MyType {
constructor( props ) {
this.state = {
panelNumber : 0,
panelDisplayMode : 'SINGLE'
}
}
}
// create a collection of those type constants
export default class MyStore extends React.Component{
MyCollection = [];
constructor () {
super();
}
componentWillMount(){
var nonsense = new MyType;
nonsense.panelNumber = 123;
nonsense.panelDisplayMode = "SINGLE";
this.MyCollection.push(nonsense);
nonsense = new MyType;
nonsense.panelNumber = 456;
nonsense.panelDisplayMode = "DOUBLE";
this.MyCollection.push(nonsense);
nonsense = new MyType;
nonsense.panelNumber = 789;
nonsense.panelDisplayMode = "TRIPLE";
this.MyCollection.push(nonsense);
}
// pass one of the instances (array) of the collection to a component.
render () {
return(
<div>
<MyComponent panel={this.MyCollection[2]}/>
</div>
)
}
}
// access one of the properties of the instance (array).
const MyComponent = props => {
console.log("props ", props);
return (
<div>
<p>MODE:</p>
{ props.panel.panelDisplayMode }
</div>
);
}
类MyType{
构造器(道具){
此.state={
编号:0,
panelDisplayMode:“单一”
}
}
}
//创建这些类型常量的集合
导出默认类MyStore扩展React.Component{
MyCollection=[];
构造函数(){
超级();
}
组件willmount(){
var=新的MyType;
无意义。面板编号=123;
displaymode=“SINGLE”;
这个。我收集。推(胡说);
无意义=新MyType;
无意义。面板编号=456;
无意义。panelDisplayMode=“DOUBLE”;
这个。我收集。推(胡说);
无意义=新MyType;
无意义。面板编号=789;
无意义。面板显示模式=“三重”;
这个。我收集。推(胡说);
}
//将集合的一个实例(数组)传递给组件。
渲染(){
返回(
)
}
}
//访问实例(数组)的一个属性。
常量MyComponent=props=>{
控制台日志(“道具”,道具);
返回(
模式:
{props.panel.panelDisplayMode}
);
}
使用setState
函数更新您的状态,并映射您的集合数组以将单个集合传递给组件
class MyStore extends React.Component{
constructor(props) {
this.state = {
MyCollection: [],
}
}
componentWillMount(){
var myCollection = [];
var nonsense = new MyType;
nonsense.panelNumber = 123;
nonsense.panelDisplayMode = "SINGLE";
myCollection.push(MyPanel);
nonsense = new MyType;
nonsense.panelNumber = 456;
nonsense.panelDisplayMode = "DOUBLE";
myCollection.push(MyPanel);
nonsense = new MyType;
nonsense.panelNumber = 789;
nonsense.panelDisplayMode = "TRIPLE";
myCollection.push(MyPanel);
this.setState({MyCollection:myCollection});
}
// pass one of the instances (array) of the collection to a component.
render () {
<div>
{this.state.MyCollection.map(
singleCollection=>{
<MyComponent panel={singleCollection}>
}
)}
</div>
}
}
// access one of the properties of the instance (array).
const MyComponent = props => (
<div>
<p>MODE:</p>{ props.panel.panelDisplayMode }
</div>
);
类MyStore扩展React.Component{
建造师(道具){
此.state={
MyCollection:[],
}
}
组件willmount(){
var myCollection=[];
var=新的MyType;
无意义。面板编号=123;
displaymode=“SINGLE”;
myCollection.push(MyPanel);
无意义=新MyType;
无意义。面板编号=456;
无意义。panelDisplayMode=“DOUBLE”;
myCollection.push(MyPanel);
无意义=新MyType;
无意义。面板编号=789;
无意义。面板显示模式=“三重”;
myCollection.push(MyPanel);
this.setState({MyCollection:MyCollection});
}
//将集合的一个实例(数组)传递给组件。
渲染(){
{this.state.MyCollection.map(
singleCollection=>{
}
)}
}
}
//访问实例(数组)的一个属性。
常量MyComponent=props=>(
模式:{props.panel.panelDisplayMode}
);
编辑
我在修复了一些语法错误后运行了这段代码。你有什么具体的错误吗?顺便说一下,这是代码
class MyType {
constructor( props ) {
this.state = {
panelNumber : 0,
panelDisplayMode : 'SINGLE'
}
}
}
// create a collection of those type constants
export default class MyStore extends React.Component{
MyCollection = [];
constructor () {
super();
}
componentWillMount(){
var nonsense = new MyType;
nonsense.panelNumber = 123;
nonsense.panelDisplayMode = "SINGLE";
this.MyCollection.push(nonsense);
nonsense = new MyType;
nonsense.panelNumber = 456;
nonsense.panelDisplayMode = "DOUBLE";
this.MyCollection.push(nonsense);
nonsense = new MyType;
nonsense.panelNumber = 789;
nonsense.panelDisplayMode = "TRIPLE";
this.MyCollection.push(nonsense);
}
// pass one of the instances (array) of the collection to a component.
render () {
return(
<div>
<MyComponent panel={this.MyCollection[2]}/>
</div>
)
}
}
// access one of the properties of the instance (array).
const MyComponent = props => {
console.log("props ", props);
return (
<div>
<p>MODE:</p>
{ props.panel.panelDisplayMode }
</div>
);
}
类MyType{
构造器(道具){
此.state={
编号:0,
panelDisplayMode:“单一”
}
}
}
//创建这些类型常量的集合
导出默认类MyStore扩展React.Component{
MyCollection=[];
构造函数(){
超级();
}
组件willmount(){
var=新的MyType;
无意义。面板编号=123;
displaymode=“SINGLE”;
这个。我收集。推(胡说);
无意义=新MyType;
无意义。面板编号=456;
无意义。panelDisplayMode=“DOUBLE”;