Javascript 如何从无渲染类React Native调用函数
假设我有A类和B类Javascript 如何从无渲染类React Native调用函数,javascript,reactjs,react-native,redux,Javascript,Reactjs,React Native,Redux,假设我有A类和B类 class A extends Component{ render() { return null; } foo = (a) => { return a; } } ... export default connect(mapStateToProps, mapDispatchToProps)(A); 然后在类B中调用foo() import A from 'path/to/A'; class B
class A extends Component{
render() {
return null;
}
foo = (a) => {
return a;
}
}
...
export default connect(mapStateToProps, mapDispatchToProps)(A);
然后在类B中调用foo()
import A from 'path/to/A';
class B extends Component{
let a;
hey() {
A.foo(a);
}
}
它给了我
TypeError:\u A.default不是构造函数
我几乎什么都试过了
newa().foo(A)代码>但不起作用
static foo=()=>…
但是在这种情况下
它看不到A类道具(A类道具未定义)connect()
上的默认值等编辑 假设我的
foo()
函数与全局状态和分派类似
foo = () => {
let length = this.props.prod.length;
let id = this.props.prod[length-1].id + 1;
this.props.addProd({name: 'LOADING', id: id});
}
此.props.prod
来自全局状态。
而this.props.addProd()
是动作创建者,负责将prod
推送到全局状态
如果我不使用类来定义这个函数。如何在此函数中调用actionCreator或获取全局状态?首先,您不需要制作一个可以像函数一样导出的组件,然后在B中调用它:
export function foo(a) {
return a;
}
然后在B中调用该函数
import { foo } from 'path/to/A';
class B extends Component{
let a;
hey() {
foo(a);
}
}
如果您需要在foo函数中使用redux state,您可以将pas作为参数,或者我建议使用redux sagas。首先,您不需要制作一个组件,您可以像函数一样简单地导出它,然后在B中调用它:
export function foo(a) {
return a;
}
class A extends Component{
render() {
return null;
}
foo = (a) => {
return <b>a</b>;
}
}
class B extends Component{
//let a;
hey() {
return new A().foo("a");
}
render() {
return this.hey()
}
}
var a = new A().foo("b")
console.log(a);
var b = new B().hey()
console.log(b);
export default function App() {
return (
<div className="App">
<B/>
<br />
{new B().hey()}
</div>
);
}
然后在B中调用该函数
import { foo } from 'path/to/A';
class B extends Component{
let a;
hey() {
foo(a);
}
}
如果您需要在foo函数中使用redux state,您可以将pas作为参数,或者我建议使用redux sagas。class a扩展组件{
class A extends Component{
render() {
return null;
}
foo = (a) => {
return <b>a</b>;
}
}
class B extends Component{
//let a;
hey() {
return new A().foo("a");
}
render() {
return this.hey()
}
}
var a = new A().foo("b")
console.log(a);
var b = new B().hey()
console.log(b);
export default function App() {
return (
<div className="App">
<B/>
<br />
{new B().hey()}
</div>
);
}
render(){
返回null;
}
foo=(a)=>{
返回a;
}
}
类扩展组件{
//让我们一起来;
嘿(){
返回新的A().foo(“A”);
}
render(){
把这个还给我
}
}
var a=新的a().foo(“b”)
控制台日志(a);
var b=new b().hey()
控制台日志(b);
导出默认函数App(){
返回(
{new B().hey()}
);
}
在jsx中使用
创建一个实例
如果
作为redux连接类导出,则可以使用redux。A类扩展组件{
render(){
返回null;
}
foo=(a)=>{
返回a;
}
}
类扩展组件{
//让我们一起来;
嘿(){
返回新的A().foo(“A”);
}
render(){
把这个还给我
}
}
var a=新的a().foo(“b”)
控制台日志(a);
var b=new b().hey()
控制台日志(b);
导出默认函数App(){
返回(
{new B().hey()}
);
}
在jsx中使用
创建一个实例
如果
导出为redux连接类-它可以使用redux。我想单独导出函数,但它会给出错误。无法导出类内的函数。导出函数不需要类。移除类,就像我在文件A put export funcion foo(A){return A;}中所做的那样,但如何处理该类中的大量dispathes?有很多的调度,我正在做的东西基于全局状态。我需要更多的细节,以便能够理解你所说的大量调度是什么意思(请张贴你的组件)。我添加了编辑部分。谢谢你的支持,我想单独导出函数,但它给出了错误。无法导出类内的函数。导出函数不需要类。移除类,就像我在文件A put export funcion foo(A){return A;}中所做的那样,但如何处理该类中的大量dispathes?有很多的调度,我正在做的东西基于全局状态。我需要更多的细节,以便能够理解你所说的大量调度是什么意思(请张贴你的组件)。我添加了编辑部分。感谢您在代码中的支持hey()
函数由于newa()
而给出错误。无法从类创建新对象。它给出了一个\默认值不是构造函数
。我的类不在同一个.js文件中。我使用的是export default
上的connect()
,如果需要使用redux renderless,那么就不需要组件/响应。。。只需在你的代码中搜索redux vanilla js
,hey()
函数就会给出错误,因为newa()
。无法从类创建新对象。它给出了一个\默认值不是构造函数
。我的类不在同一个.js文件中。我使用的是export default
上的connect()
,如果需要使用redux renderless,那么就不需要组件/响应。。。只需谷歌搜索redux vanilla js