gpt4 book ai didi

typeahead.js - 为什么 React.js 不能与 Twitter 的 Typeahead 库一起使用

转载 作者:行者123 更新时间:2023-12-01 12:35:36 24 4
gpt4 key购买 nike

我正在为我的应用程序使用 React,我注意到当我尝试在 React 组件内部使用时,Twitter Typeahead 库不起作用。我知道那里有很多其他的 React 自动完成库,但我想知道为什么它不能与 Twitter 的 typeahead 一起使用,而 React 已知可以与 jQuery 和多个其他库一起使用

更新:

作为示例,我使用了 fb avatar 示例代码并尝试使用 typeahead。

<!DOCTYPE html>
<html>
<head>
<title>Hello React</title>
<script src="https://fb.me/react-0.13.2.js"></script>
<script src="https://fb.me/JSXTransformer-0.13.2.js"></script>
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="http://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"></script>
</head>
<body>
<div id="example"></div>


<div>
<div id="bloodhound">
<input class="typeahead" type="text" placeholder="States of US"/>
</div>
</div>

</body>

<script type="text/jsx">


var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
];

// constructs the suggestion engine
var states = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
// `states` is an array of state names defined in "The Basics"
local: states
});



$('#bloodhound .typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'states',
source: states
});



var Avatar = React.createClass({
render: function() {
return (
<div>
<ProfilePic username={this.props.username} />
<ProfileLink username={this.props.username} />
<div id="bloodhound">
<div><input className="typeahead" type="text" placeholder="States of US"/></div>
</div>
</div>
);
}
});

var ProfilePic = React.createClass({
render: function() {
return (
<img src={'https://graph.facebook.com/' + this.props.username + '/picture'} />
);
}
});

var ProfileLink = React.createClass({
render: function() {
return (
<a href={'https://www.facebook.com/' + this.props.username}>
{this.props.username}
</a>
);
}
});

React.render(
<Avatar username="sports" />,
document.getElementById('example')
);
</script>
</html>

在组件外部工作的框在 React 组件中呈现时不是同一个框。我原来的问题与此非常相似,因此我使用了这个例子

最佳答案

请查看这个更新的答案

<html>
<head>
<title>Hello React</title>
<script src="https://fb.me/react-0.13.2.js"></script>
<script src="https://fb.me/JSXTransformer-0.13.2.js"></script>
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="http://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"></script>
<link rel="stylesheet" type="text/css" href="http://twitter.github.io/typeahead.js/css/examples.css">
</head>
<body>
<div id="example"></div>


<div>
<div id="bloodhound">
<input class="typeahead" type="text" placeholder="States of US"/>
</div>
</div>

</body>

<script type="text/jsx">


var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
];

// constructs the suggestion engine
var states = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
// `states` is an array of state names defined in "The Basics"
local: states
});



$('#bloodhound .typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'states',
source: states
});



var Avatar = React.createClass({
handleChange: function(e) {
console.log(e.target.value);
},

componentDidMount: function() {
React.findDOMNode(this.refs.myTextInput).focus();
$ (React.findDOMNode(this.refs.myTextInput)).typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'states',
source: states
});
},

render: function() {
console.log('resd');
return (
<div>
<ProfilePic username={this.props.username} />
<ProfileLink username={this.props.username} />
<div id="bloodhound">
<div>
<input className="typeahead" type="text" placeholder="States of US" ref="myTextInput" name="sahan" onChange={this.handleChange} onBlur={this.handleChange} /></div>
</div>
</div>
);
}
});

var ProfilePic = React.createClass({
render: function() {
return (
<img src={'https://graph.facebook.com/' + this.props.username + '/picture'} />
);
}
});

var ProfileLink = React.createClass({
render: function() {
return (
<a href={'https://www.facebook.com/' + this.props.username}>
{this.props.username}
</a>
);
}
});

React.render(
<Avatar username="sports" />,
document.getElementById('example')
);
</script>
</html>

关于typeahead.js - 为什么 React.js 不能与 Twitter 的 Typeahead 库一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30158084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com