[英]React/Meteor: How to get data from component to its associated container?
I'm getting the data for my List component from a container. 我正在从容器中获取列表组件的数据。 So far everything is fine.
到目前为止,一切都很好。
There is an input field and a list of items (files). 有一个输入字段和一个项目(文件)列表。 Right now there are thousands items and it doesn't make sense to show them all.
现在有成千上万的项目,把它们全部显示出来是没有意义的。 Thats why I would like to use a search field to get only matching items.
这就是为什么我想使用搜索字段仅获取匹配项的原因。
Typing a value to the input field will use search()
, but now I need to get this value into the container element to get the specific data. 在输入字段中键入一个值将使用
search()
,但是现在我需要将此值添加到容器元素中以获取特定数据。
How do I get the value into the 'parent' container? 如何将值放入“父”容器中? Or maybe this kind of search has to be done completely different?
还是必须完全不同地进行这种搜索?
/containers/list.js /containers/list.js
import { createContainer } from 'meteor/react-meteor-data'
import List from '../components/List.jsx'
export default createContainer(() => {
const subscription = Meteor.subscribe('files.all') // <-- better get specific data..., so use search term
const files = Files.find().fetch()
return { files }
}, List)
/components/List.jsx /components/List.jsx
export default class List extends Component {
search (event, data) {
const term = data.value
console.log(term) // <-- term has to be used in the container...?
}
render () {
const { files } = this.props
const filesList = files.map((file, key) => {
return (
<ListFile
key={'file' + key}
file={file}
/>
)
}) || null
return (
<div id='list'>
{
filesList &&
<div>
<Input onChange={this.search.bind(this)}/>
<Item.Group>
{ filesList }
</Item.Group>
</div>
}
</div>
)
}
}
Two options that I can think of: 我可以想到两个选择:
1) Use Session (Meteor specific option). 1)使用会话(流星特定选项)。 2) Use Redux (Can be used in general).
2)使用Redux(可以一般使用)。
Since you are using Meteor and it is out of the box there, I would advise to use Session. 由于您正在使用Meteor,并且开箱即用,因此我建议您使用Session。
In both parent and child import Session 在父子导入会话中
import { Session } from "meteor/session"; 从“流星/会话”导入{会话};
In parent createContainer, do something like 在父级createContainer中,执行以下操作
const searchQuery = Session.get("searchQuery");
apply searchQuery in subscription or in Files.find() as you wish
return { files, searchQuery }
In child component, do something like 在子组件中,执行类似
search (event, data) {
const term = data.value
console.log(term)
Session.set("searchQuery", term);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.