简体   繁体   English

为什么不在另一个组件中运行类组件中的函数? 反应本机

[英]Why isnt running the function from class component in another component? REACT-NATIVE

I have a stacknavigator and in headerTitle have a header component for each screen, heres the code:我有一个 stacknavigator 并且在headerTitle有每个屏幕的标题组件,代码如下:

 const Home_stack = createStackNavigator({ //hooks
    
    Home: {
        screen: Home, 
        navigationOptions: ({navigation}) => {
            return {
            headerTitle: () => <Header navigation = {navigation} title = "Shum Note"/>}
        }
    },
    Create: {
        screen: Create, 
        navigationOptions: ({navigation}) => {
            return {
            headerTitle: () => <Childs_header navigation = {navigation} title = "Create Note"/>}
        }
    },
    Edit: {
        screen: Edit, 
        navigationOptions: ({navigation}) => {
            return {
            headerTitle: () => <Childs_header navigation = {navigation} title = "Edit Note"/>}
        }
    },
});

and this is the component Childs_header:这是 Childs_header 组件:

import Create_note from "../components/Create_note";

class Header extends Component {

    comun = new Create_note();

    render() {
        return (
            <>
                <View style ={{backgroundColor: "white", flexDirection: "row", alignItems: "center"}}>
                    <View>
                        <Text style = {{color: "black", fontSize: 30, marginLeft: -20}}>{this.props.title}
                        </Text>
                    </View>
                    <View>
                        <Text>
                            <Feather name="check-square" size={24} color="black" onPress = {() => this.comun.save_data(this.props.navigation)}/>
                        </Text>
                    </View>
               </View>
            </>
        );
    }
}

export default Header;

as you can see I import the component Create_note and create an object of it to use one of its function, in this case save_data , but for some reason it isnt working, dont know if it has something to do with AsyncStorage becase with console.log("hi") it works, but saving data it doesnt, heres the structure of create_note component:如您所见,我导入了组件Create_note并创建了它的一个对象以使用其功能之一,在这种情况下是save_data ,但由于某种原因它不起作用,不知道它是否与AsyncStorageAsyncStorageconsole.log("hi")它可以工作,但不能保存数据,这是 create_note 组件的结构:

class Create_note extends Component {

state = {
    content: "",
    default_color: "#87cefa", //default color (cyan)
}

save_data = async() => {
    
    if (this.state.content === "") {
    
        //navigation.navigate("Home");
    
    }else {
        
        let clear_content = this.state.content.replace(/&nbsp;/g,""); //replace al &nbsp;
        
        try {

            const data = await AsyncStorage.getItem("data");
    
            if (data === null) {
                const data = {"array_notes": [], "last_note": 0}; 
                const last_note = data.last_note + 1;
                const new_note = {note_number: last_note, content: clear_content, color: this.state.default_color, text_color: this.state.color}; //create a new_note object, note_number will be the key for each note
                const array_notes = [];
                array_notes.push(new_note);
                data.array_notes = array_notes;
                data.last_note = last_note;
                await AsyncStorage.setItem("data", JSON.stringify(data)); //using stringify to save the array 
                //navigation.navigate("Home");
            }else {
                const data = JSON.parse(await AsyncStorage.getItem("data")); //use parse to acces to the data of the array
                const last_note = data.last_note + 1;
                const new_note = {note_number: last_note, content: clear_content, color: this.state.default_color, text_color: this.state.color};
                const array_notes = data.array_notes;
                array_notes.push(new_note);
                data.array_notes = array_notes;
                data.last_note = last_note;
                await AsyncStorage.setItem("data", JSON.stringify(data));
                //navigation.navigate("Home");
            } 
    
        }catch(error) {
            alert(error);
        }
    }
}

render() {
    const props = {
        screen: "create_note",
        change_color: this.change_color.bind(this),
        update_color: this.update_color.bind(this),
    }
    return (
        <>
            <ScrollView>                    
                <RichEditor
                ref = {this.richText}
                onChange = {text => this.setState({content: text}, () => console.log(this.state.content))}
                allowFileAccess = {true}>
                </RichEditor>
            </ScrollView>
            {this.state.change_color ? 
                <Color
                    {...props}>
                </Color>
            : null}
            <RichToolbar
                editor = {this.richText}
                onPressAddImage = {this.insertImage}
                actions = {[
                    actions.insertBulletsList,
                    actions.insertOrderedList,
                    actions.insertImage,
                    "change_text_color", 
                ]}
                iconMap  ={{
                    [actions.insertBulletsList]: () => <Text style = {this.styles.icon}><MaterialIcons name = "format-list-bulleted" size = {this.option_icon.size} color = {this.option_icon.color}/></Text>,
                    [actions.insertOrderedList]: () => <Text style = {this.styles.icon}><MaterialIcons name = "format-list-numbered" size = {this.option_icon.size} color = {this.option_icon.color}/></Text>,
                    [actions.insertImage]: () => <Text style = {this.styles.icon}><MaterialIcons name = "image" size = {this.option_icon.size} color = {this.option_icon.color}/></Text>,
                    change_text_color: () => <Text style = {this.styles.icon}><MaterialIcons name = "format-color-text" size = {this.option_icon.size} color = {this.option_icon.color}/></Text>,
                }}
                change_text_color = {this.change_color}
                style = {{backgroundColor: "white"}}>
            </RichToolbar>
            <Button title = "save" onPress = {this.save_data}></Button>
        </>
    );
}

heres an image so you can see better the structure:这是一张图片,以便您可以更好地看到结构:

在此处输入图片说明

the function should run when I click in the check icon, in the blue button works because its part of the create_note component, but I want it in the check icon当我单击check图标时,该功能应该运行,蓝色按钮起作用,因为它是 create_note 组件的一部分,但我希望它在check图标中

From looking at your code I think the problem is that you're passing the navigation state object as a parameter to your save_data function in the onClick of your checkmark.通过查看您的代码,我认为问题在于您将导航状态对象作为参数传递给您的save_data标记onClicksave_data函数。

this.comun.save_data(this.props.navigation)

but the function definition of save_data doesn't take any parameters:但是save_data的函数定义不带任何参数:

save_data = async () => {
  // ...
};

So you could change the save_data function to something like this所以你可以将save_data函数更改为这样的

save_data = async (navigation) => {
  // ...
};

in order to have it work from inside the Header component.为了让它从Header组件内部工作。


If you want the save button, rendered by the Create_note component, to also call save_data onPress ;如果您希望Create_note组件呈现的保存按钮也调用save_data onPress you will have to pass the navigation state there as well.您还必须在那里传递导航状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 React-native 中从另一个组件更改一个组件的状态 - Changing the state of one component from another component in React-native React-Native:从父组件调用组件内的 function - React-Native: Calling a function within a component from parent component in react-native:将完整的STATE从一个组件发送到另一个组件? - react-native: send complete STATE from one component to another? 是否可以从 react-native 中的类组件访问功能上下文? - Is it possible to access functional context from class component in react-native? 来自另一个组件的 (react-native) 函数不会返回我的值 - (react-native) function from another component won't return my value React-native传递给另一个组件 - React-native passing component to another 从react-native的Navigator按钮调用组件函数 - Call a component function from react-native's Navigator buttons React-native:使用其他组件中的“ this”导出功能 - React-native: Export function using 'this' from other component 从 React-Native 中的不同屏幕组件在 App 中调用 function - Calling a function in App from a different Screen component in React-Native 在 class 组件 react-native 中声明 const - declaring const in class component react-native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM