ここにはすばらしい回答がたくさんありますが、私はReactにはかなり新しいので、私の状況に適用するのが難しいです。コンポーネントがあり、そのコンポーネントにはWebサイトの訪問者に関するデータを保持する単一のレコードが渡されます。これらのフィールドの1つでは、外部ファイルに存在する関数を適用したいと考えています。以下のコードは未定義ReactJsコンポーネント内の小道具で関数を実行する
ActivityTable.jsのプロパティを読み取ることができませんChromeデベロッパーツールで実行時エラー 'prettyUserAgent' を与え
import React, { Component } from 'react';
import { StringManipulation } from '../../helpers/stringManipulation';
export class ActivityTableItem extends Component {
constructor() {
super();
this.state = { userAgent: '' }
}
render() {
return (
<tr onClick={(e) => { e.preventDefault(); }}>
<td>
<div>{ this.props.activity.websiteUri }</div>
</td>
<td>
<div>{ this.props.activity.ipAddress }</div>
</td>
<td>
<div className="small text-muted">{ StringManipulation.prettyUserAgent(this.props.activity.userAgent) }</div>
</td>
<td className="text-center">
<img src={'/img/flags/USA.png'} alt="USA" style={{height: 24 + 'px'}}/>
</td>
<td>
<div>{ this.props.activity.createdAt }</div>
</td>
</tr>
)
}
}
StringManipulation.js
import UAParser from 'ua-parser-js';
// Create a pretty string to display main details from a user agent
export const prettyUserAgent = (str) => {
const parser = new UAParser();
const result = parser.setUA(str).getResult();
const browser = result.browser;
return result.browser;
};
私は理解して、なぜこのエラーその関数が存在する前にデータに対して呼び出されていますが、それを修正する方法は呼び出されていません。私はcomponentDidMountなどを使用することであらゆる種類のバリエーションを試しましたが、運が全くありません。