react-native - 如何将 props 从组件传递到另一个组件

标签 react-native react-redux

我有两个组件,一个在另一个内部呈现,父级有一些 child 需要的 Action ,但我不知道如何传递它

最后的控制台日志,我需要将操作传递给 child ,因为 child 需要操作

问题是 child 已经在同一个屏幕上呈现,所以我不知道如何传递这些信息

父组件半径:

render(){
    console.log("radiuscomp ", this.props)
    return(
        <View>
            <View>
                <AppHeaderBar onLeftButtonPress = {this.pop} title = "Radius"/>
            </View>
            <ScrollView
            style = {{height: this.state.visibleHeight}}
            keyboardShouldPersistTaps = 'always'
            >
                {this.updateContent()}
                <SearchLocation /> //this is the child component 
            </ScrollView>
        </View>
    )
}

function mapStateToProps(state){
    return {
       updateLocationList: state.taskListReducer.consumerTaskLocationHistoryEvent
    };
}

function mapDispatchtoProps(dispatch){
   return bindActionCreators(actions,dispatch);
}

let SearchRadiusContainer = connectEnhance(mapStateToProps,mapDispatchtoProps)(SearchRadiusComponent);

子组件位置:
render(){
    return(
        <View>
            <View style = {Styles.search}>
                <TouchableOpacity onPress={() => this.requestLocationPermission()}>
                    <Image style = {{width: 30, height: 30}} source = {require('../_shared/components/Images/map.png')} />
                </TouchableOpacity>
                <SearchBox
                ref={ref => (this.autoCompleteInput = ref)}
                onEndEditing={this.dismissLoading}
                onChangeText={text => this.googlePlaceAutoCompletion(text)}
                style={Styles.searchbox}
                placeholder="Suburb or postcode"
                />
            </View>
            {this.alternateHistoryKeywords()}
        </View>
    )
}

function mapStateToProps(state){
    return {
        updateLocationList: state.taskListReducer.consumerTaskLocationHistoryEvent
    };
}

function mapDispatchtoProps(dispatch){
   return bindActionCreators(actions,dispatch);
}

let SearchLocationContainer = connectEnhance(mapStateToProps,mapDispatchtoProps)(SearchLocationComponent);

由于子组件中的此代码保存文本输入值,因此子组件需要父组件的操作
saveToHistory(){
    let here = false
    this.state.searchLocationList.filter((find, index) => {
        if(find.toLowerCase() == this.state.searchLocation.toLowerCase()){
            here = true
        }
    })
    if(here == false){
        this.setState({
            searchLocationList: [this.state.searchLocation, ...this.state.searchLocationList]
        },()=> this.props.consumerTaskLocationHistoryEventAction(this.state.searchLocationList))  //this action is on parent but not on child
    }
}

Console Logs

最佳答案

请不要使用 redux 将 props 从父级传递给子级。你需要知道你有智能组件(连接到 redux store)和普通组件(对 redux 一无所知)。

你的代码应该是这样的

state = {
  lon: 54.002,
  lat: 34.990,
}
render() {
  return (
     <Parent>
        <Child {...this.props} lat={this.state.lat} lon={this.state.long} />
     </Parent>
  );
}
{...this.props}创造一个魔法,你所有的父 Prop 都会被传递给一个子组件。只需将 redux 连接到父组件并将 {...this.props} 添加到子组件

关于react-native - 如何将 props 从组件传递到另一个组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46640220/

相关文章:

javascript - Mapbox React Native 无法加载样式

javascript - 在 React Native 中链接 CSS 框架

javascript - 在react router v4中渲染子路由

typescript - 在 React Redux 中保存选定的日期而不是 utc 日期

javascript - React-Redux - 创建一个隐藏容器的 Action

react-native - 无法使用 detox 运行测试用例

javascript - 是否可以在 React Native 中将 configureScene 的 PushFromRight 更改或添加到 PushFromLeft?

caching - cacheEntryRemoved promise 何时实现?

javascript - React/Redux 如何传递参数来修改 API

reactjs - 使用 Material-UI 的数组中的多个悬停弹出窗口仅打开一个弹出窗口