Reactjs 反应:如何访问其类外的道具对象?

Reactjs 反应:如何访问其类外的道具对象?,reactjs,jestjs,enzyme,Reactjs,Jestjs,Enzyme,我不知道如何在接收孩子的上下文之外访问props对象,有什么方法吗?这里是我设置的一个最小的测试文件(Jest和Ezyme)来进行实验。我在尝试将this.props.propNumber传入时出现语法错误:“这是一个保留字” const React=require(“React”); 常量酶=需要(“酶”); 常量适配器=需要(“酶-适配器-反应-16”); configure({adapter:newadapter()}); //采用增量法的简单构件 类Foo扩展了React.Compone

我不知道如何在接收孩子的上下文之外访问props对象,有什么方法吗?这里是我设置的一个最小的测试文件(Jest和Ezyme)来进行实验。我在尝试将
this.props.propNumber
传入时出现语法错误:
“这是一个保留字”

const React=require(“React”);
常量酶=需要(“酶”);
常量适配器=需要(“酶-适配器-反应-16”);
configure({adapter:newadapter()});
//采用增量法的简单构件
类Foo扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
测试:1
};
}
handleIncrement=(incrementSize)=>{
这是我的国家({
testIncrement:this.state.testIncrement+=递增大小
})
};
render(){
返回(
试验
);
}
}
常量包装器=酶。浅(
);
测试('prop passage into shall instance',()=>{
wrapper.instance().handleIncrement(this.props.propNumber);
expect(wrapper.state('testIncrement')).toEqual(6);
});

您知道道具是什么,因为您正在传入道具,为什么需要从类中访问道具

const incrementSize = 1;
const propNumber = 5;

const wrapper = enzyme.shallow(
  <Foo
    propNumber={propNumber}
  />
);

test('prop passage into shallow instance', () => {
  wrapper.instance().handleIncrement(incrementSize);
  expect(wrapper.state('testIncrement')).toEqual(propNumber + incrementSize);
});
const incrementSize=1;
常数propNumber=5;
常量包装器=酶。浅(
);
测试('prop passage into shall instance',()=>{
wrapper.instance().handleIncrement(incrementSize);
expect(wrapper.state('testIncrement')).toEqual(propNumber+incrementSize);
});

而且你的
handleIncrement
函数不接受数组,因此没有理由传递它。

你知道道具是什么,因为你要传递它,为什么你需要从类中访问它

const incrementSize = 1;
const propNumber = 5;

const wrapper = enzyme.shallow(
  <Foo
    propNumber={propNumber}
  />
);

test('prop passage into shallow instance', () => {
  wrapper.instance().handleIncrement(incrementSize);
  expect(wrapper.state('testIncrement')).toEqual(propNumber + incrementSize);
});
const incrementSize=1;
常数propNumber=5;
常量包装器=酶。浅(
);
测试('prop passage into shall instance',()=>{
wrapper.instance().handleIncrement(incrementSize);
expect(wrapper.state('testIncrement')).toEqual(propNumber+incrementSize);
});

而且您的
handleIncrement
函数不接受数组,因此没有理由传递数组。

在父组件文件中,导出值,然后在子组件文件中,在父组件文件中导入值,导出值,然后在子组件文件中,在我的代码中导入值

,我试图模拟从父组件传递一个道具,作为子方法中的参数(5的
propNumber
将设置
incrementSize
本地的
handleIncrement()
)。我认为您的代码不会复制特定的模式,除非我遗漏了一些东西您的代码试图测试组件中的特定函数-它正确地更改了组件的状态。你不需要模拟整个React生命周期就可以做到这一点。在我的代码中,我试图模拟从父组件传递一个道具,作为子方法中的参数(5的
propNumber
将为
handleIncrement()
设置
incrementSize
local)。我认为您的代码不会复制特定的模式,除非我遗漏了一些东西您的代码试图测试组件中的特定函数-它正确地更改了组件的状态。你不需要模拟整个生命周期就可以做到这一点。