私は最初のアプリケーションを開発していて、まだフローを学習しています。次の例のような方法のHelloWorld()を保持している子から親コンポーネントのメソッドを呼び出す - ネイティブを返す
親::
import React, { Component } from 'react';
class Parent extends Component {
Helloworld() {
console.log('Hello world');
}
render() {
return (
<View>{this.props.children}</View>
)
}
}
module.exports = Parent;
、その後、私は別のコンポーネントにして、これをインポートする方法と、その方法を使用したい は、だから私はと呼ばれるコンポーネントを持っていると仮定します私はそれをするのですか? 私はそれを実装する方法の別の短い例を書いています。
import React, { Component } from 'react';
import { Parent } from 'path to parent';
//or
const Parent = require('path to parent');
//which of these is better?
class Home extends Component {
Helloworld() {
console.log('Hello world');
}
render() {
return (
<Parent>
// this is what i need
<Button onClick={parent.Helloword()}>Some Button</Button>
</Parent>
)
}
}
module.exports = Home;
ありがとうございました。
私に戻ってくれてありがとう。私はこれをテストする時間がなかったが、私は急いであなたにフィードバックを与える。 – TheMan68
インポートに関しては別の違いがあります。言いたいことを忘れてしまいました。 'import'はファイルの先頭でしか使用できませんが、' require'はどこでも使用できます。 – lalkmim
ここでの回答はどちらもうまくいきますが、私はこれがもう少し私が必要とするものに合わせていることを知ります。どうもありがとうございました – TheMan68