私はEggheadを "最初の制作品質のリアクションアプリを構築"しています。レッスン17まで、すべてがうまくいっています。context
を使用して自宅を作りますルータコンポーネント。私の知る限り、私は授業中とまったく同じことを行うが、私はLink
コンポーネントのいずれかをクリックしたときに、私はこのコンソールのエラーを取得:nullのプロパティ 'context'を読み取ることができません
Link.js:11 Uncaught TypeError: Cannot read property 'context' of null
at handleClick (http://localhost:3000/static/js/bundle.js:33792:12)
at Object.ReactErrorUtils.invokeGuardedCallback (http://localhost:3000/static/js/bundle.js:17162:17)
at executeDispatch (http://localhost:3000/static/js/bundle.js:16945:22)
at Object.executeDispatchesInOrder (http://localhost:3000/static/js/bundle.js:16968:6)
at executeDispatchesAndRelease (http://localhost:3000/static/js/bundle.js:16356:23)
at executeDispatchesAndReleaseTopLevel (http://localhost:3000/static/js/bundle.js:16367:11)
at Array.forEach (native)
at forEachAccumulated (http://localhost:3000/static/js/bundle.js:17265:10)
at Object.processEventQueue (http://localhost:3000/static/js/bundle.js:16570:8)
at runEventQueueInBatch (http://localhost:3000/static/js/bundle.js:24192:19)
リンクコンポーネントは以下のようになります。
import React, { Component } from 'react';
export class Link extends Component {
static contextTypes = {
route: React.PropTypes.string,
linkHandler: React.PropTypes.func,
}
handleClick(e) {
e.preventDefault();
this.context.linkHandler(this.props.to)
}
render() {
const activeClass = this.context.route === this.props.to ? 'active' : '';
return <a href="#" className={activeClass} onClick={this.handleClick}>{this.props.children}</a>
}
}
Link.propTypes = {
to: React.PropTypes.string.isRequired
}
およびルータのコンポーネントは以下のようになります。
import React, { Component } from 'react';
const getCurrentPath =() => {
const path = document.location.pathname;
return path.substring(path.lastIndexOf('/'));
}
export class Router extends Component {
state = {
route: getCurrentPath()
}
handleLinkClick = (route) => {
this.setState({ route }); // same as { route: route }
history.pushState(null, '', route);
}
static childContextTypes = {
route: React.PropTypes.string,
linkHandler: React.PropTypes.func,
};
getchildContext() {
return {
route: this.state.route,
linkHandler: this.handleLinkClick,
};
}
render() {
return <div>{this.props.children}</div>
}
}
問題を引き起こして何ができるかの任意のアイデア?
正しい方向のポインタをありがとう!
EDIT:
した後、私が得たアドバイスを、次の、私はコンストラクタで(矢印関数で同じ結果を)をバインドし、期待通りに関数が呼び出されることを確認したが、今私が手(ありがとう!)別のエラー:
Uncaught TypeError: this.context.linkHandler is not a function
at Link.handleClick (http://localhost:3000/static/js/bundle.js:33707:21)
at Object.ReactErrorUtils.invokeGuardedCallback (http://localhost:3000/static/js/bundle.js:17162:17)
at executeDispatch (http://localhost:3000/static/js/bundle.js:16945:22)
at Object.executeDispatchesInOrder (http://localhost:3000/static/js/bundle.js:16968:6)
at executeDispatchesAndRelease (http://localhost:3000/static/js/bundle.js:16356:23)
at executeDispatchesAndReleaseTopLevel (http://localhost:3000/static/js/bundle.js:16367:11)
at Array.forEach (native)
at forEachAccumulated (http://localhost:3000/static/js/bundle.js:17265:10)
at Object.processEventQueue (http://localhost:3000/static/js/bundle.js:16570:8)
at runEventQueueInBatch (http://localhost:3000/static/js/bundle.js:24192:19)
あなたはhandleClickにこれをバインドする必要があります。これは、コンストラクタやonClick(推奨しません)で行うことができます。 onClick = {this.handleClick.bind(this)}。または、handleClickの矢印関数を使用します。 const handleClick =()=> {}。 –
^^これは疑問の答えです。 – Jason