【发布时间】:2017-10-31 16:56:44
【问题描述】:
给定:
Welcome.js
import React from 'react';
import WorkPlacePage from '../../components/welcome/WorkPlacePage';
import SkillPage from '../../components/welcome/SkillPage';
class Welcome extends React.Component {
constructor(props) {
super(props);
switch (props.match.params.welcomeId) {
case "workplace":
this.state = { step: 1 };
break;
case "skills":
this.state = { step: 2 };
break;
default:
this.state = { step: 1 };
break;
}
console.log(this.state)
}
nextStep(props) {
console.log('nextStep')
console.log(this)
this.setState({
step : this.state.step + 1
})
}
showStep(props) {
const {history} = this.props
switch (this.state.step) {
case 1:
return <WorkPlacePage history={history}
nextStep={this.nextStep} />
case 2:
return <SkillPage history={history}
nextStep={this.nextStep} />
default:
return (
<div>
<h1>Case: Default</h1>
</div>
);
}
}
render() {
var style = {
width : (this.state.step / 4 * 100) + '%'
}
return (
<main>
<span className="progress-step">Step {this.state.step}</span>
<progress className="progress" style={style}></progress>
{this.showStep()}
</main>
)
}
}
export default Welcome;
WorkPlacePage.js
import React from 'react';
import createBrowserHistory from 'history/createBrowserHistory';
class WorkPlacePage extends React.Component {
saveAndContinue = (e) => {
//e.preventDefault()
this.props.nextStep()
}
render() {
const history = createBrowserHistory();
return (
<div>
<h1>Workplace</h1>
<span>
survey things
// <button onClick={() => history.push('/welcome/skills')}>next page</button>
</span>
<button onClick={() => this.saveAndContinue() }>XXX page</button>
</div>
);
}
}
export default WorkPlacePage;
当我点击该按钮时,我收到以下this.props.nextStep() 的错误
Uncaught TypeError: Cannot read property 'props' of null
我做错了什么?
【问题讨论】:
标签: reactjs react-router react-redux react-router-redux