Answer the question
In order to leave comments, you need to log in
How to enforce two way communication between components in ReactJS?
Hello, I
have a class and a function:
import React from 'react';
export default class ResultsContainer extends React.Component {
constructor(props) {
super(props);
this.state = {
//хочу получить в state ответ с myFunction
response: ''
};
}
componentDidMount = () => {
const a=1;
myFunction(a);
}
render() {
return (
<div>
...
</div>
)
}
}
const myFunction = (props) => {
const {a} = props;
const response = a++
return (
response
)
}
Answer the question
In order to leave comments, you need to log in
1. Declaring componentDidMount as an arrow function is insanity. It doesn't make any sense.
2. If your function is executed synchronously, it is enough to reduce to:
const response = myFunction(1);
this.setState({ response });
componentDidMount() {
const { someValue } = this.props;
// асинхронный вызов, возвращающий Promise
fetchSomeData(someValue).then(response => this.setState({ response }));
}
async componentDidMount() {
const { someValue } = this.props;
// асинхронный вызов, возвращающий Promise
const response = await fetchSomeData(someValue);
this.setState({ response }));
}
const myFunction = num => ++num;
componentDidMount = () => {
const a=1;
this.setState({response: myFunction(a)});
}
async componentDidMount = () => {
const a=1;
this.setState({response: await myFunction(a)});
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question