将param传递给另一个具有反应的组件

时间:2017-10-25 11:28:16

标签: reactjs react-native

我正在尝试将参数传递给名为Cell with custom的自定义组件。这是我的代码

<Cell cellTitle='test' style={styles.item}></Cell>

In Cell

constructor(props) {
    super(props);
    const cellTitle = props.cellTitle;
    console.log(cellTitle);
  }

  render() {
    return (
      <Text style={styles.title}>{cellTitle}</Text>. // I get the error here
    )
}

我收到错误

Can't find variable cellTitle

2 个答案:

答案 0 :(得分:2)

在构造函数中,您将cellTitle赋给const变量

const cellTitle = props.cellTitle;

一旦构造函数完成执行,该变量将不再存在。

因此,要么将其分配给state,要么直接在渲染方法中使用this.props.cellTitle

答案 1 :(得分:1)

您已将cellTitle声明为构造函数中的const。 这在渲染函数中是未知的。

您可以在渲染中使用道具:

render() {
    return <Text style={styles.title}>{this.props.cellTitle}</Text>;
}
相关问题