最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Trouble passing props to componentDidMount in child component (React) - Stack Overflow

programmeradmin0浏览0评论

I'm having issues passing a prop to a ponentDidMount() call in a child ponent on my React application.

In my App.js I am passing props via Router like below:

App.js

class App extends Component {

state = {
    city: ""
}

ponentDidMount() {
    this.setState({city: this.props.city});
}

render() {

    return (
        <div>
            <Route path="/" exact render = {() => <Projections city={this.state.city} />} />
            <Route path="/:id" ponent={FullPage} />
        </div>
    );
}

}

In my Projections.js I have the following:

Projections.js

constructor(props) {
        super(props);
           this.state = {
            location: this.props.city
        }
    }

    ponentDidMount () {
        console.log(this.state.location);
console.log(this.props.city);

    }

console.log(this.state);' returns an empty string.console.log(this.props.city);` returns an empty string as well.

But I need to access the value of the city prop within ponentDidMount(). console.log(this.props.city); within render() returns the prop, but not in ponentDidMount()

Why is this and how do I return props within ponentDidMount()?

I'm having issues passing a prop to a ponentDidMount() call in a child ponent on my React application.

In my App.js I am passing props via Router like below:

App.js

class App extends Component {

state = {
    city: ""
}

ponentDidMount() {
    this.setState({city: this.props.city});
}

render() {

    return (
        <div>
            <Route path="/" exact render = {() => <Projections city={this.state.city} />} />
            <Route path="/:id" ponent={FullPage} />
        </div>
    );
}

}

In my Projections.js I have the following:

Projections.js

constructor(props) {
        super(props);
           this.state = {
            location: this.props.city
        }
    }

    ponentDidMount () {
        console.log(this.state.location);
console.log(this.props.city);

    }

console.log(this.state);' returns an empty string.console.log(this.props.city);` returns an empty string as well.

But I need to access the value of the city prop within ponentDidMount(). console.log(this.props.city); within render() returns the prop, but not in ponentDidMount()

Why is this and how do I return props within ponentDidMount()?

Share Improve this question edited Jul 6, 2018 at 13:31 logos_164 asked Jul 6, 2018 at 13:11 logos_164logos_164 7861 gold badge14 silver badges33 bronze badges 1
  • 2 You say that the first console.log returns an empty string, then the second one return nothing as well. An empty string is not nothing ;) Especially when you do state = { city: "" } – sjahan Commented Jul 6, 2018 at 13:15
Add a ment  | 

2 Answers 2

Reset to default 3

In the constructor you should reference props, not this.props:

location: props.city
        <Route path="/" exact render = {() => <Projections city={this.state.city} {...this.props} />} />

Try passing rest of props in route

this is because you assigned props in constructor that time it may or may not receive actual value. And it gets called only once in a ponent lifecycle. You can use ponentWillReceiveProps to get props whenever it receive and update state accordingly.

Inside Projections.js

UNSAFE_ponentWillReceiveProps(nextProps){
   if(nextProps.city){
     this.setState({location:nextProps.city})
   }
}

Here is working codesand

发布评论

评论列表(0)

  1. 暂无评论