将图标从fontaweslyicon上的功能(on Change文本输入)更改
我正在考虑根据之前的用户交互更改使用 fontawesome-react 包设置的图标。
用户在屏幕上有一个输入字段和按钮(其中包含一个图标)。如果输入为空,则
会显示不同的图标无用户输入:
+-----------------------+-----------+
| Search Placeholder... | (Shuffle) |
+-----------------------+-----------+
用户输入:
+-----------------------+-----------+
| Value | (Search) |
+-----------------------+-----------+
在
内手动设置图标时, 我能够找到faSearch
,但是当使用状态设置它时,我收到错误:
Could not find icon {prefix: 'fas', iconName: 'faSearch'}
这是我到目前为止的看法。如果有人能够提供帮助并能够解释我所缺少的内容,我会非常高兴。
class Search extends React.Component {
state = {
displayIcon: "faSearch",
}
onChange(event) {
console.log(event.target.value)
if(event.target.value === ''){
this.setState({
displayIcon: "faShuffle",
})
} else {
this.setState({
displayIcon: "faSearch",
})
}
}
render(){
const { displayIcon } = this.state //destucture state
console.log( );
return (
<>
<InputGroup className="mb-3">
<FormControl
placeholder="search or feel lucky..."
onChange={this.onChange}
/>
<Button variant="primary" id="search" size="lg">
<FontAwesomeIcon icon={displayIcon} />
</Button>
</InputGroup>
</>
)
}
}
I'm thinking about changing the icon which is set using the fontawesome-react package depending on a previous user interaction.
On screen the user has a input field and button (which contains an icon). If the Input is empty a different icon is presented
No User Input:
+-----------------------+-----------+
| Search Placeholder... | (Shuffle) |
+-----------------------+-----------+
User Input:
+-----------------------+-----------+
| Value | (Search) |
+-----------------------+-----------+
when setting the icon manually inside the <FontAwesomeIcon>
I'm able to find faSearch
, When however setting it using state I get the error:
Could not find icon {prefix: 'fas', iconName: 'faSearch'}
Here's my take so far. I'd be really happy if someone is able to assist and is able to explain what I'm missing.
class Search extends React.Component {
state = {
displayIcon: "faSearch",
}
onChange(event) {
console.log(event.target.value)
if(event.target.value === ''){
this.setState({
displayIcon: "faShuffle",
})
} else {
this.setState({
displayIcon: "faSearch",
})
}
}
render(){
const { displayIcon } = this.state //destucture state
console.log( );
return (
<>
<InputGroup className="mb-3">
<FormControl
placeholder="search or feel lucky..."
onChange={this.onChange}
/>
<Button variant="primary" id="search" size="lg">
<FontAwesomeIcon icon={displayIcon} />
</Button>
</InputGroup>
</>
)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您也可以这样做并有条件地渲染它:
you can also do that like this and render it conditionally:
也许您可以尝试为
fa-search
传递一个字符串,而不是传递一个对象,如下所示:onChange
中的实现是:Maybe instead of passing an object you could try to pass a string for
fa-search
like so:<FontAwesomeIcon icon='fas fas-search
/>The implementation in
onChange
being: