Input React JSX:如何将道具设置为占位符属性
我有一个输入标记,我正在尝试将占位符的内容设置为组件的道具。编译JSX并在浏览器中运行后,占位符根本不会显示。它也不会抛出任何错误。我该怎么做Input React JSX:如何将道具设置为占位符属性,input,reactjs,placeholder,react-jsx,Input,Reactjs,Placeholder,React Jsx,我有一个输入标记,我正在尝试将占位符的内容设置为组件的道具。编译JSX并在浏览器中运行后,占位符根本不会显示。它也不会抛出任何错误。我该怎么做 <input type="text" onChange={this.props.handleChange} placeholder={this.props.name} /> 子组件中的此代码似乎没有问题。占位符应该在您实现它时显示得很好 以下是我如何在父级中设置它: import React, { Component } from 'rea
<input type="text" onChange={this.props.handleChange} placeholder={this.props.name} />
子组件中的此代码似乎没有问题。占位符应该在您实现它时显示得很好 以下是我如何在父级中设置它:
import React, { Component } from 'react';
import Title from'./Title';
import TestList from'./TestList';
export default class Layout extends Component {
constructor() {
super();
this.state = {
title: 'Moving Focus with arrow keys.',
placeholder:'Search for something...'
};
}
render() {
return (
<div >
<Title title={ this.state.title } />
<p>{ this.getVal() }</p>
<TestList placeholderText={this.state.placeholder} />
</div>
);
}
}
import React,{Component}来自'React';
从“./Title”导入标题;
从“/TestList”导入测试列表;
导出默认类布局扩展组件{
构造函数(){
超级();
此.state={
标题:“使用箭头键移动焦点。”,
占位符:'搜索某物…'
};
}
render(){
返回(
{this.getVal()}
);
}
}
以下是我在子对象中显示它的方式:
import React, { Component } from 'react';
export default class TestInput extends Component {
constructor(props){
super(props);
};
render() {
return (
<div>
<input type="search" placeholder={this.props.placeholderText} />
);
}
}
}
import React,{Component}来自'React';
导出默认类TestInput扩展组件{
建造师(道具){
超级(道具);
};
render(){
返回(
);
}
}
}
回复有点晚,但希望能有所帮助!:-) 另一个答案是:
父组件
import React, { Component } from 'react';
import TextView from'./TextView';
export default class DummyComponent extends Component {
constructor() {
super();
this.state = {
};
}
render() {
return <TextView placeholder = {"This is placeholder"} />
}
}
import React,{Component}来自'React';
从“/TextView”导入TextView;
导出默认类DummyComponent扩展组件{
构造函数(){
超级();
此.state={
};
}
render(){
返回
}
}
童装
import React, { Component } from 'react';
export default class TextView extends Component {
constructor(props){
super(props);
};
render() {
const { placeholder } = this.props;
return <input type="text" placeholder = {placeholder} />
}
}
import React,{Component}来自'React';
导出默认类TextView扩展组件{
建造师(道具){
超级(道具);
};
render(){
const{placeholder}=this.props;
返回
}
}
jsbin-谢谢!我的道具设置不正确。